Week_01-Learn Dsa With c++
Week_01-Learn Dsa With c++
LEARN DSA
WITH C++
Introduction To Programming
10 years ago, Once upon a time there was a man who had 5 pet animals such as 2 Goats, 2 Cows and
one Dog. He was counting these animals using stone. He sees an animal and holds a stone. Thus he
counted those animals.
Then time changed, people invented decimal numbers for calculations. Decimal numbers are 0 to 9
(0,1,2,3,4,5,6,7,8, 9). Now people can count from 0 to 1000. But that time was impossible.
Now consider one man doing business. He has four notebooks for calculation. And every notebook
has 1000 pages. Now, if you want to calculate those notebooks one by one. Again it's impossible. If you
calculate it’s a high chance to make a mistake.
Then, Charles Babbage credited a computer called a mechanical computer in 1822. Basic meaning of
Computer - To Calculator.
That time, A decimal number was used on the computer. Computer size was one room.
A few years later, the transistor was invented. A transistor is a semiconductor device used to amplify or
switch electrical signals and power. Transistors provide two numbers 0 and 1 (Binary number). Generate
1 when the transistor is on and 0 when the transistor is off.
Decimal Calculation:-
15 47 56
+ 34 +26 +3 2
—--------- —------ —------
49 73 88
Binary Calculation:-
0 0 1 1
+ 0 + 1 + 0 + 1
—------------- —------------- —------------- —-------------
0 1 1 10
Decimal To Binary :
0 1 2 3 4 5 6 7
1 0 1 1 0
1 X 2⁴ 0 X 2³ 1 X 2² 1 X 2¹ 0 X 2⁰
16 0 4 2 0
Result = 16 + 0 + 4 + 2 + 0 = 22
Exp:- 2 (1 1 0 1 0 1)
1 1 0 1 0 1
1 X 2⁵ 1 X 2⁴ 0 X 2³ 1 X 2² 0 X 2¹ 1 X 2⁰
32 16 0 4 2 1
Result = 32 + 16 + 0 +4 + 2 +1 = 55
2 35
2 17 1
2 8 1
2 4 0
2 2 0
2 1 0
2 0 1
2 67
2 33 1
2 16 1
2 8 0
2 4 0
2 2 0
2 1 0
2 0 1
Result = 1 0 0 0 0 1 1
Transistor ::
One transistor 2 value 1 either 0.
0 1
1 0
0 0 0 1 1 0 1 1
0000 0001 0010 0011 010 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
0
Moore's Law : Moore's law is the observation that the number of transistors in an integrated
circuit (IC) doubles about every two years.
Simple meaning: If there is a transistor in one place. After 2 years there should be 2 transistors in the
same place.
Now, we use high level languages like C++, Java, and Python. And the compiler or interpreter converts
the code into binary code.
High Level Language:: A high-level language is any programming language that enables
development of a program in a much more user-friendly programming context and is generally
independent of the computer's hardware architecture.
Assembler: Assembler is a program for converting instructions written in low-level assembly code
into relocatable machine code and generating long information for the loader.
Importance of Algorithms?
Using Algorithms, our code executes very fast and takes less space for store.
Time Complexity: Total time taken by the algorithms with respect to input size.
Space Complexity: the amount of memory space required to solve an instance of the
computational problem as a function of characteristics of the input.
Importance of DSA?
1. Take less Time
2. Take less space
FLOWCHART
Exp:- 01 How did you decide whether to go for a walk or stay alone on a rainy day
FLOWCHART PSEUDO CODE
1. Read number
2. number * number * number
3. Output cube
1. See Letter
2. Take decision according to package
3. Package suitable so accept or not
satisfy don't go.
1. Read num
2. Check remainder
3. Remainder is 0 = Even
4. Remainder is not 0 = Odd
5. Print result.
1. Read num
2. Take variable sum = 0, int = 1
3. Check condition int <= n
4. If break loop Print sum
1. Read num
2. Number divided by 2
3. Check condition num < divider
4. Agine check condition num%divider
=0
5. It is 0= print Not Prime, 0 != divider
increase 1
6. When condition divider < num false
print Prime
1 byte = 8 bit
0 1 0 1 1 0 0 1
Memory Unit:
1 Byte 1 KB 1 MB 1 GB
Binary number:
Computers understand only binary numbers.
INTRODUCTION TO C++
C++ was developed as an extension of C, and both languages have almost the same syntax.
The main difference between C and C++ is that C++ supports classes and objects, while C does
not.
First code::
int(integer) = 1, 3, 5,
Float(Floating Point )= 2.1, 3.1
Double( Double Floating Point)= 0.123, 1.0235
Alphabet :- A,V,R,E,J
char(Character )= A,V,R,E,J
bool (Boolean) = Yes/no true/false
Differents types of Data Types:
VARIABLE ::
Variables are containers for storing data values.
int num = 500;
#include<iostream> #include<iostream>
using namespace std; using namespace std;
return 0; return 0;
}; };
Comment :- Comments can be used to explain code, and to make it more readable. Complier is not execute
the comment.
// This is a comment Single line comment
/* The code below will print the words Hello World!
to the screen, and it is amazing */ Multiline Comment
sizeof() :- The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type.
Output : 65 Output : A
Here, At first the compiler converts the code into binary numbers. Then, Binary numbers convert into output
according to ASCII TABLE.
IF-ELSE Condition
The C++ if statement tests the condition. It is executed if the condition is true.
Comparison Operators:
== Equal to x==y
int main()
{
int first_num = 20;
int second_num = 30;
if(first_num>second_num)
{
cout<<first_num;
}
else
{
cout<<second_num;
}
return 0;
};
int main()
{
int num = 25;
if(num%2==0)
{
cout<<"EVEN";
}
else
{
cout<<"ODD";
}
return 0;
};
Exp :: 03 Check number Positive, Negetive or Zero?
#include <iostream>
using namespace std;
int main()
{
int num = 0;
if(num>0)
{
cout<<"It is Positive Number";
}
else if(num<0)
{
cout<<"It is Negative Number";
}
else
{
cout<<"It is zero Number";
}
return 0;
};
Logical Operator::
&& ⇒ Logical AND return True if both statements are true x<5 && x < 10
|| ⇒ Logical OR return True if one statements are true x<5 || x < 4
! ⇒ Logical NOT Reverse the result return false if the result is true !( x<5 && x < 10)
int main()
{
int a= 20, b= 40, c= 60;
HOMEWORK
int main()
{
int year;
cout<<"Enter Your Year: ";
cin>>year;
#include <iostream>
using namespace std;
int main()
{
char c = 'h';
if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u')
cout<<"Vowel";
else
cout<<"Consonant";
/*if(c=='a')
cout<<"Vowel";
else if(c=='e')
cout<<"Vowel";
else if(c=='i')
cout<<"Vowel";
else if(c=='o')
cout<<"Vowel";
else if(c=='u')
cout<<"Vowel";
else
cout<<"Consonant"; */
return 0;
};
int main()
{
int num = 27;
if(num%3==0 && num%5==0)
cout<<"Perfect";
else
cout<<"Not Perfect";
return 0;
};
TAKE INPUT FROM USER cin>>”Your Value”;
Exp :: 03 :: Add two numbers and Take input from the user?
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout<<"Enter num1: ";
cin>>num1;
cout<<"Enter num2: ";
cin>>num2;
int sum;
sum=num1+num2;
LOOP
A loop statement allows us to execute a statement or group of statements multiple times.
For loop::
for(initialize; condition; operation) initialize : This statement gets executed only once, at the
{ beginning of the for loop.
// code Condition: This statement use for break the loop when condition
false
Operation: It is used for updating a number.( Num++ ← Increase)
};
int main()
{
for(int num=1; num<=10; num++)
cout<<num<<" ";
return 0;
};
num ++ —>> Post Increment
++num —>> Pre increment
Why use :: ++num, num++, --num, num-- ⇒ less memory used.
num++ :- ++num
num = 0 num = 0
sum = num ++ sum = ++num
[store old value then increment] [store new value]
sum=0 sum = 1
(Store old value)
int main()
{
for(int num=1; num<=20; num++)
{
if(num%2==0)
cout<<num<<" ";
}
return 0;
};
HOMEWORK
int main()
{
int n;
cout << "Enter Number: ";
cin >> n;
if(n<2)
{
cout<<"Not a Prime";
return 0;
}
int main()
{
int num=3;
return 0;
};
int main()
{
int n;
cout << "Enter Number: ";
cin >> n;
int first_num = 0;
int second_num = 1;
int current_num;
if (n == 1)
{
cout << 0;
return 0;
}
if (n == 2)
{
cout << 1;
return 0;
}
return 0;
};
int main()
{
int n = 3 > 2 > 1;
cout << n;
return 0;
};
3 << >> ‘’
4 < <= > >= ‘’
5 == != ‘’
6 && ‘’
7 || ‘’
int main()
{
int n = 3*2 - 10/5;
cout << n;
return 0;
};
Nested Loop:: Nested loop means a loop statement inside another loop statement.
include <iostream>
int main()
int n;
return 0;
};
PATTERN PRINTING
int main()
{
int n;
for (int row = 1; row <= 5; row++)
{
for (int col = 1; col <= 5; col++)
cout << col << " ";
cout << endl;
}
return 0;
};
int main()
{
int n;
#Code::
#include <iostream>
using namespace std;
int main()
{
int n;
int counter=1;
cout<<counter<<" ";
counter++;
}
cout<<endl;
}
return 0;
};
#Code::
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter Star Row: ";
cin>>n;
cout<<"*"<<" ";
}
cout<<endl;
}
return 0;
};
int main()
{
int n;
cout<<"Enter Star Row: ";
cin>>n;
cout<<col<<" ";
}
cout<<endl;
}
return 0;
};
#Code
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter Star Row: ";
cin>>n;
cout<<endl;
}
return 0;
};
int main()
{
int n;
cout<<"Enter Star Row: ";
cin>>n;
cout<<endl;
}
return 0;
};
HOMEWORK
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < i; j++)
cout << " ";
cout << j + 1;
}
cout << endl;
}
return 0;
};
PATTERN PRINTING + WHILE LOOP + MATHS
*
**
***
****
*****
#Explanation:
2 3 = 2 * row -1 space = 4 - 2
3 5 = 2 * row -1 space = 4 - 3
4 7 = 2 * row -1 space = 4 - 4
Code:-
#include <iostream>
using namespace std;
int main()
{
int Total_rows;
cout << "Enter star number: ";
cin >> Total_rows;
// for space
for (int col = 1; col <= Total_rows - row; col++)
cout << " ";
// For Star
for (int col = 1; col <= 2 * row - 1; col++)
cout << "*";
return 0;
};
#Explanation:
row - 1 star- 1 space-4
(Nrow) (Trow-Nrow)
2 2 3
3 3 2
4 4 1
5 5 0
Nrow = row num; Trow = Total row Inner Space = Cout<<”* space”;
Code :-
#include <iostream>
using namespace std;
int main()
{
int Totat_rows;
cout << "Enter Row Number: ";
cin >> Totat_rows;
return 0;
};
#Explanation:
# Upper Half Star:
Left side star: For space : right side star:
row star 2 * n -- 2*row same with left side
1 1
2 2 (Total col - row star) (depend on row)
3 3 ( star=row)
4 4
5 5
(star=row)
# Lower Half Star:
int main()
{
int n;
//Total_row = n
cout<<"Enter The row Num: ";
cin>>n;
// upper side
for(int row =1; row<=n; row++)
{
// star
for(int col=1;col<=row;col++)
cout<<"*";
// space
for(int col=1;col<=2*n-2*row;col++)
cout<<" ";
// star
for(int col=1;col<=row;col++)
cout<<"*";
cout<<endl;
// lower side
for(int row =1; row<=n-1; row++)
{
// star
for(int col=1;col<= n - row; col++)
cout<<"*";
// space
for(int col=1;col<=2*row; col++)
cout<<" ";
// star
for(int col=1;col<= n - row; col++)
cout<<"*";
cout<<endl;
return 0;
};
WHILE LOOP
0 1
00 2
00000 5
Code::
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int num=10;
int digit =1;
return 0;
};
while(Condition) Condition is True code is executed. When the condition is False, the loop is broken.
{
// #code
};
Same Q. Exp: 01 :: Find digit any number like( 125, 220056, 65487 );
#Explanation: Divided by 10 until we get 0 result.
#Code::
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter Your Digit: ";
cin>>n;
int Digit = 0;
while (n)
{
Digit++;
n =n/10;
}
cout<<Digit;
return 0;
};
#Explanation:
1st part Get digit reverse one by one. 2nd part Multiply by 10 and add digits one by one.
Divide by 10 and get remainder 0 * 10 + 5 = 5
3265/10 %5 5 * 10 + 6 = 56
326/10 % 6 56 * 10 + 2 = 562
32/10 % 2 562 * 10 + 3 = 5623
3/10 % 3
#Code::
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter Number: ";
cin>>num;
int sum = 0;
while(num)
{
int digit = num%10;
sum = sum*10+digit;
num = num/10;
}
cout<<sum;
return 0;
};
Exp: 03:: Convert Decimal to Binary. 37 Output: (100101)
#Explanation:
1st Part- 2nd Part- 3rd Part-
Divide by 2 and get remainder (%) According to Ex:2 According to Ex:2
One by one bin*10 + bin Reverse
#Code::
#include<iostream>
using namespace std;
int main()
{
int num, sum=0, mul=1;
cout<<"Enter Your Numbers: ";
cin>>num;
while(num>0)
{
int digit = num%2;
sum = sum + digit *mul;
num= num/2;
mul = mul*10;
};
cout<<sum;
return 0;
};
NOTE**
When input long values like (1345664). It is overflow (32bit to 64bit). So, use a long long data type.