Cpp with STL NOTES
**********************
what is computer?
electronic machine takes input and process it gives output
hardware and software
binary language
0 and 1
language
hindi grammer
english grammer
french
....
..
c language garmmer
progarm -> .c -> .o ->.exe(machine langauge) 01010100001101
double click-> (Run)
binary language
cpp language
java
python
.java
.py
...
10000+
.c/.cpp --we read this file only
.exe -we cant able read this file -01010101010000101111
C language middle level programming lang.
Histroy of C language
ide-----code editor attachhed compiler
console -Black screen
application software - web application, console-based applications
hardware - camera open program - in which program is iteracting with hardware
hello
int a ;
.c - source file
Build process-Preprocessor -
compiler -
Linker -
Headers - why we are using headers file
#
.exe file- executable file
.jpg - exe file
.pdf - exe file
.txt - exe file
########################################################
Begin C langauge
Grammer
I aare is
| - hindi
Basic letters
int a; full stop (;)
a to z -
A to Z
0 to 9
& ^ % ! ( ) { } _ + =[] < ; > :~? - " "
Tokens == words:- constants, variables, keywords
Instructions == sentence :-Declaration instruction
-Action instruction
cammand == instructions == statement
********************************
Syntax == Grammer ==vyakaran
token == word
sentence == instructions
program = essay or paragraph
program - set of instructions
constant = information == data
1.Primary constants --- Integer ,Real ,character
2.Secondary constants - array ,string ,pointer ,structure ,union ,enuerator
****************************
Integer - the number who doesnot contain points 1,3,574453,6783453,-86,0
Real - the number who must have points 0.2,2.3443,2.0,-0.34,
character -Any symbol can be a character if single code is implemented there
ex - 'A' ,'a', '%', '1'
Very very important ***
''-character
""-string
C language is Case Sensitive**
___________________________________________
mobile number 7,903,789,361- Integer constant
mobile balance - 100.23 - Real constant
how many brothers and sisters you have - INteger constant
what is the name of your city - secondary constant (string)
"wasdjhfkjlasdhfkjh" - string -"" double codes
++++++++++++++++++++++++++++++++++++++++++++
variables and keywords
**********************************08/03/2024***********************
VARIABLES:- Variables are the names of memory locations where we can store our data
or informations;
int a; 'a' ->4 bytes 00000000 00000000 00000000 00000100
CPU -> MU ,CU,ALU
In MU (memory unit ) there is full or registers (RAM)
Registes is capacitors Lakhs of capacitors are there
if capacters is charged then it is points to 1
not charged then 0
ROM (Hard disc) - lifetime
1 byte = 8 bit
1024 byte = 1 kb
1024 kb = 1 mb
1024 mb = 1 gb
2^32
________________________-----------------------------___________________________
Variables are the combinations of alphabet, digit and underscore.
only alphabet is correct ex-a,b,c as,As,bk,
only digit or only underscore is not correct ex _, 9,574,
combinations ex-a_2, A2,A_3,a_B,
NOte **No other symbol is permited**
**Valid variable name cannot start with a digit. 1a,3b45,3_b; WRONG
In programs ,we need to define or declare variable first
headers
int main(){
int a,b,c;
return 0;
}
___________________________*************************_______________________________
____________________________
Keywords:-
predifined keywords:- tokens ==meaningful words
auto,break,case,char,const,continue,default,do,double,else,enum,extern,float,for,go
to,if,int,long
register,return,short,signed,sizeof,static,struct,switch,typedef,union,unsigned,voi
d,volatile,while
Int double char float -
int -Integer constant 4 bytes 32 bits
int a- int is keyword which is used for declaring a variable who can only holds the
integer value(contants);
a==(integer)
double b; 8 bytes
float f; 4 bytes double is keyword which is used for declaring a variable who can
only holds the real value(contants);(points)
The doubles and floats are datatypes used to declare decimal type variables. They
are similar, but doubles have 15 decimal digits, and floats only have 7.
char a2,a3; 1 byte if We put any letter inside the single code then it become
character.
char a; valid
bool -True || any non-zero value is true || any non-empty string or character is
true; || 1 as ture we are using commonly
false -0||'',""
false
Datatypes:-
predefined datatypes/built-in data types/primitive datatypes:-
int
char
double
float
void is also a datatypes
user-defined datatypes:-
custom datatypes (structure, class,)
__________________________________
ASCII code
'A'-65
'a'-97
char a;
a='A';
%c a
typecasting :- int to double 2 to 2.0
double to int data loss 2.34 ---- 2
int b='A';
printf("value of b is %d",b) ===65
print("%c",b);=='A'
______________________________________________________________________________
Declaration statement :-
int a;
double d;
char c;
float f;
Action statement:-
Action :-
3 types of action statements:-
Input output instructions;
Arithmetic instruction
control instructions
**********************************09/03/2024*****************
input and output instructions:-
input => scanf - predefined function
take input from the console
int a; %d
double -%lf
float - %f
char - %c
int a;
scanf("%d",&a);
double d;
scanf("%lf",&d);
input()
output => printf - predefined function
able to print on the console
number of letters=printf("%d",a);
printf("value of a is %d\n",a);
Escape sequences
\n - new line
\t = tab space
\b - backspace
\r - carriage return
\\ - print \
\" - print "
\' - print '
int a =3,c=10;
//scanf("%d",&a);
=printf("Hello world\n");
printf("%d",a);
printf("value of a is %d \n %d",a,c);
******************CODE**************
#include<stdio.h>
int main()
{
int a =3,c=10;
//scanf("%d",&a);
printf("Hello world\n");
printf("%d",a);
printf("value of a is %d \n %d",a,c);
printf("Hello\ncoding club\n");
printf("Hello\tcoding club\n");
printf("Hello\bcoding club\n");
printf("Hello \\coding club\n");
return 0;
}
***********************************
format specifiers
%d - int
%c - char
%f -float
%lf -double
Arithmetic instructions:-An instruction which is used to manipulate data using
operators is known as Arithmetic instruction.
operations - processs
operator - +,-,/,*,%,^,|,&
operands - data
2+3= 5 process is operation
2,3 are operands
+ is operator
5 is result
BODMAS rule is not followed here in c lang.
percidence rule is followed
3+4*5 = 23 by bodmas 4* ,4
Types of operator: On the basis of precidence rule
Unary operator -> - +,-,++,--,sizeof()
Arithmetic operator -> +,-,*,/,%(Modulas) (remainder find)
Bitwise operator
Relational operator
Logical operator
Conditional operator
Assignment operator =
Types of operator on the basis of number operands
unary operator
binary operator
ternary operator- : ?
Unary operator :- +,-,++,--,sizeof()
+,- define the number either positive of negative sign denote
sizeof() - size of any datatypes or any variables
increment(++) or decrement(--)
pre increment (++a)or post increment(a++)
pre decrement (--a)or post decrement(a--)
+++,--- wrong
5++ wrong
+5,-5,sizeof()
sizeof(int); =4 bytes // Datatypes
double a;
sizeof(a);= 8 // variables
#include<stdio.h>
int main()
{
int a=5;
a++; // a=a+1; 6
int b=a;
int c=++a; // a=7,c=7
int d=a++; //d=7,a=8
printf("%d",sizeof(int));
printf("a= %d\n",a); //8
printf("b= %d\n",b); //6
printf("c= %d\n",c); //7
printf("d= %d\n",d); //7
return 0;
}
//++a, b=++a // 7
//b=a then ++a;
assign = post incr decre
there is no difference in working of post and pre incremennt or decrement operator
but difference in priority
3-4+5 = 4 by bodmas
3+1 =4
-1 + 5 =4 by c lang. rule
Arithmetic operators +,-,*,/,%
same priority +,- (left to right)
same priority *,/,% (left to right) more priority than (+,-)
if you found more than one operator with same priority then u can solve from left
to right
4+ 4- 4+5+5 -6 = left to right I have solved here
8-4+5+5-6
4+5+5-6
9+5-6
14-6 =8
8/4*5 ==10 is correct in c lang.
3*4/3 == 4 is correct in c lang. in normal mathematics 3
priority level in this question is equal /==* as per priority level
division
3/4== 0
6/3.0=2.0
6.0/3.0 = 2.0(quotient)
int /int == int
float /int == float
flaot/float == float
double/double == double
int/double = double
7%3 =1
6%2==
12345 we want to get 5 from it which opertion will be required
12345%10 =5
12345/10=1234
3%4= 3-0 =3
4*0 =0
4*1=4
boolean value -> true or false
true -non-value is true like 1,233,235456,-1,323 (1), non-empty "tkjdf"
false -zero or ""
Relational operations:- <,>,<=,>= priority same
==, != ---- same priority
< - less than
> - greater than
<= - less than equal to
>= - greater than equal to
== -equal to
!= - not equal to
= equal assign
4>5 - false 0
x=2;
x==3 - false
question 1. 5>4>3 => 1>3 false (0)
5>4 = true (1)
1
5>4<3 true
Logical operators:-!(NOT) -unary
&& (AND)
|| (OR)
!true = false
!false = true
!5>-2 = false>-2 => 0>-2 = true (1)
5 = true
!true = false
!5 = false
for AND (&&) both expressions is true then it answer will be ture otherwise false
in every conditions
Exp1 && exp2 == Result
true && true == true
True && false == false
false && == false
for OR (||) atleast one of the experssion must be true for getting true results
Exp1 || exp2 == Result
false || false == false
false || true == True
true || == true
4>5 && 3<2 == false
false
4>5 || 3>2 == true
false || true
*****************************************18/03/2024*********************
Assignment Operator
= (assign)
x= 6+7*2;
x= 6+14
x=20
value of x become 20
4=x; // error
x=8668978 +909-7868
if x=2
x=x+5; => //2=2+5
x=2+5
x=7 answer
container = content + constant
find x?
COMPOUND ASSIGNMENT OPERATOR
+=,-=,*=,/=,%=,
X+=2; // X=X+2
X-=3;// X=X-3;
X*=5;// X=X*5;
X/=4;// X=X/4;
X%=3; // X=X%3;
Bitwise Operator
& AND operator
| OR operator
^ XOR OPerator
~ NOt operator
>> right shift
<< left shift
It works on the bits
AND Operator
binary numbers -0 or 1
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
OR opeator
0 | 0 =0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1
XOR operator
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0
~0 = 1
~1 = 0
answer of ~5 will be -6;
1 byte = 8 bit
~00010101
11101010
Right shift
00010101>>2====00000101
left shift
00010101<<3=====10101000
Topics covered till now
basic letters
contants , variables , keywords
instructions -- declarations
action
input output
arithmetic
control
CONTROL INSTURCTION:-
Decision control instruction
Iterative control instruction
Switch case instruction
goto instruction
Decision control instruction Or Selection control ins.
if
if else
conditional operator
main(){
code
if(condition)
{
code
code
}
code
}
Write a program to check whether a given number is positive or non-positive. input
for console .
Solution:-
#include<stdio.h>
int main(){
int x;
printf("Enter a number:");
scanf("%d",&x);
if(x>0)
{
printf("Positive number");
}
if(x<=0)
{
printf("Non-Positive number");
}
write a program to check the number either it is even or odd
all the inputs is positive
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<limits.h>
int main(){
int n;
printf("plz Enter a number : ");
scanf("%d",&n);
if(n%2==0)
{
printf("Even number");
}
else{
printf("Odd number");
}
}
""-false
0 false
1 == true
"344sdf"== ture
In if or else = either of these two only one will run at a time.
there will no else without if.
else() it is worng
if(condition);
Nested If else
void fun(){
if(condition){
if(){
}
else{
if(){
}
}
else{
if()
{
}
else{
if(){
}
else{
}
}
}
Else if ladder
void ladder_if_else(){
if(){
}
else if(){
}
else if(){
}
else if(){
}
else{
}
}
Conditional Operator:-
exp1 ? exp2 : exp3;
Write a program to check the number is divisible by 5 or not
#include<stdio.h>
int main()
{
int n,x;
printf("Enter a number: ");
printf("enter a second num: ");
scanf("%d%d",&n,&x);
n%5==0?printf("divisible by 5"):printf("not div");
if(x%2==0){
printf("\ndiv by 2");
}
else
{
printf("\nnot div by 2");
}
return 0;
}
OUtput:Enter a number: 5
enter a second num: 6
divisible by 5
div by 2
*****Iterative control instruction & repeat control instruction
1.While
2.do while
3.for
while loop - Entry control loop
print coding club 500 times
int main(){
printf("coding club");
printf("coding club");
printf("coding club");
printf("coding club");
printf("coding club");
}
while(conditions)
{
code
}
int main(){
int i=5;
while(i<5){
printf("coding club");
i++;
}
2.do while loop - exit control loop
do
{
}while(condition);
#include<stdio.h>
int main()
{
int i=3;
do{
printf("coding club %d\n",i);
i++;
}while(i<3);
printf("%d",i);
return 0;
}
3.for loop
Syntax of for loop
for(initialisation ; condition ; flow/(increment/decrement))
{
}
first 10 natural numbers;
1 2 3 4 5 6 7 8 9 10
#include<stdio.h>
int main()
{
int i=1;
for(;i<=10;i++)
{
printf("%d ",i);
}
return 0;
}
Break and Continue
Always used inside the body of loop or body of the switch.
**(break) :- it takes the control out of the loop body
while(condition)
{
code
code
if(condition)
break;
code
}
Continue : it is a keyword
it can only be used in the body of loop
it moves the control to the next iteration
while(condition)
{
code
code
if(condition)
continue;
code
}
Switch case control instruction:
switch or case - both are keyword
switch(expression)
{
case constant:
code
code
break;
case constant:
code
code
case constant:
code
default:
code
}
###CODE
#include<stdio.h>
int main(){
int x;
printf("Enter a number:");
scanf("%d",&x);
switch(x)
{
case 1..10:x>=1&& x<=10
printf("One");
break;
case 2:
printf("Two");
break;
case 3:
printf("Three");
break;
case 4:
printf("Four");
break;
case 5:
printf("Five");
break;
default:
printf("Number not in the range");
return 0;
}
goto control instruction:
goto is a keyword
we can send the control on the labbeled locations
lb2:
code
code
goto lb1;
code
code
lb1:
code
code
goto lb2:
TCS ke interview me pucha gya tha
write a code in which code of both if or else will run .
if(condition==true)
{
code
goto a;
}
else
{
a:
code
}
Star Pattern Program:-
To print a single star:
######code
#include<stdio.h>
int main(){
printf("*");
return 0;
}
output:-*
>>>>>print star four times in single line:
###code
#include<stdio.h>
int main(){
for(int i=0;i<4;i++)
{
printf("*");
}
return 0;
}
output: ****
print star four times in single line and cursor will be in the next line:
##code:
#include<stdio.h>
int main(){
for(int i=0;i<4;i++)
{
printf("*");
}
printf("\n");
return 0;
}
output:
****
__-cursor is here
print four stars in five rows:
#####code:
#include<stdio.h>
int main(){
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
/*
j= 12345
i=1 *****
i=2 *****
i=3 *****
i=4 *****
Next question is
print the given below pattern:-
*
**
***
****
*****
by default j=1,i=1
j= 12345 j
i=1 * 1 j<=1
i=2 ** 1,2 j<=2
i=3 *** 1,2,3 j<=3
i=4 **** 1,2,3,4 j<=4
i=5 ***** 1,2,3,4,5 j<=5
j<=i
create a relation with the help of relational operator
*/
#####Code:
#include<stdio.h>
int main(){
// j is
//int x;
//scanf("%d",&x);
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
if(j<=i)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
>>>print given below pattern:-
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
>>>> row column
1 2 3 4 5 i=1 1,2,3,4,5 j>=1
_ 2 3 4 5 i=2 2 3,4,5 j>=2
_ _ 3 4 5 i=3 3,4,5 j>=3
_ _ _ 4 5 i=4 4,5 j>=4
_ _ _ _ 5 i=5 5 j>=5
condition j>=i
>>>>>>
A
AB
ABC
ABCD
ABCDE
FUNCTIONS:-
1.Function is a block of code which has some name of identification
2.Function's name must be unique in a program
3.A c language program can have any number of functions
4.Function call is a representative of function code.
5.Define function only once and call it any number of times in your program
6.Even in the smallest C lang program ther should be at least one function
7.Function is a way to achieve modularization
Splitting up a bigger task into several smaller subtasks to reduce the
complexity of original task is known as modularization
8.Function are of two types:-
-Predefined function ex. printf(),scanf(),getch() etc
-User defined function -
9.No keyword is a function and No function is a keyword
10.main() is entry point of your program
11.when we call the function then function has executed(Function tab execute hota
jb usko call karte h)
function defination
function declaration
function call
#include<stdio.h>
void add();// function declaration
int main()
{
printf("Hello coding mafia");
add(4,6);// function call
//printf("%d",b);
}
// function defination
void add()
{
int a,b;
printf("Enter two numbers: ");
scanf("%d%d",&a,&b);
int c=a+b;
printf("sum is %d",c);
}
Ways to define a function
1.Takes Nothing, Return Nothing (TNRN) ex.-void add()
2.Takes Something, Return Nothing (TSRN) ex. void add(int a)
3.Takes Nothing , Return Something (TNRS) ex. int add()
4.Takes Something , Return Something (TSRS) ex. int add(int a)
#include<stdio.h>
void add();// function declaration
int main()
{
printf("Hello coding mafia");
add(4,6);// function call
//printf("%d",b);
}
// function defination
void add() //TNRN
{
int a,b;
printf("Enter two numbers: ");
scanf("%d%d",&a,&b);
int c=a+b;
printf("sum is %d",c);
}
void sub(int a,int b) //TSRN
{
//int a,b;
//printf("Enter two numbers: ");
//scanf("%d%d",&a,&b);
int c=a-b;
printf("sum is %d",c);
}
int multiplication() //TNRS
{
int a,b;
printf("Enter two numbers: ");
scanf("%d%d",&a,&b);
int c=a*b;
return c;
//return a*b;
}
int div(int a,int b) //TSRS
{
return a/b;
}
Moving to CPP
header - #include<isotream>
using namespace std;
printf- cout<<
scanf - cin>>
new line - endl;
Write a code for addition like sum of two numbers which in the type (TSRN)
>>>>Solution:-
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void add(int a, int b );
int main() {
// Your code goes here;
add(4,6);
return 0;
}
void add(int a, int b ){
int c = a+ b;
cout<<c;
}
By Default int datatypes are there in the return type of any function;
#include<stdio.h>
add(int a, int b );
int main() {
// Your code goes here;
int d=add(5,6);
printf("%d",d);
return 0;
}
add(int a, int b ){
int c = a+ b;
return c;
}
Recursion in c lang:
the process in which the program repeats a certain section of code in a similar
way.
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int result = factorial(5);
printf("The factorial of 5 is %d\n", result);
return 0;
}
5! = 1 * 2 *3*4*5=120
4!= 1 *2 *3*4 = 24
5!= 4!*5 =24*5=120
1!= 1
0!= 1
Output:-The factorial of 5 is 120
*****ExPlanation**************
#include <stdio.h>
int factorial(int n) { //5
if (n == 0) { // termination
return 1;
} else {
return 5 * factorial(n - 1);
}
}
// int factorial(int n) { //4
// if (n == 0) { // termination
// return 1;
// } else {
// return 4 * 6//factorial(n - 1);//3 =n-1
// }
// }
// int factorial(int n) { //3
// if (n == 0) { // termination
// return 1;
// } else {
// return 3 * 2//factorial(n - 1);//2
// }
// }
// int factorial(int n) { //2
// if (n == 1) { // termination
// return 1;
// } else {
// return 2 * 1//factorial(n - 1);//1
// }
// }
// int factorial(int n) { //1
// if(n == 0) { // termination
// return 1;
// } else {
// return 1 * 1//factorial(n - 1);//0
// }
// }
// int factorial(int n) { //n=0
// if (n == 0) { // termination // base case
// return 1;
// } else {
// return 0 * factorial(n - 1);//4
// }
// }
int main() {
int result = factorial(5);
printf("The factorial of 5 is %d\n", result);
return 0;
}
***End*******
#include <stdio.h>
int fibonacci(int n) {
if (n == 0 || n == 1) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
int main() {
int result = fibonacci(10);
printf("The 10th Fibonacci number is %d\n", result);
return 0;
}
output:-The 10th Fibonacci number is 55
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...
sum of numbers up to n:-
int f1(int x) {
int s;
if(x==1)
return 1;
s=x+f1(x-1);
return s;
}
#include<stdio.h>
int f1(int x) //3
{
int s;
if(x==1)
return 1;
s=3+3//f1(x-1); // f1(3-1)== f1(2)
return s;
} // 1+2+3
int f1(int x) //2
{
int s;
if(x==1)
return 1;
s=2+1//f1(x-1); // f1(2-1)== f1(1)
return s;
}
int f1(int x) //1
{
int s;
if(x==1)
return 1;
s=x+f1(x-1);
return s;
}
int main(){
int k;
k=f1(3)//6;
printf("%d",k);
}
print n natural numbers:-
#include<stdio.h>
void printn(int n) // 5
{
if(n==1)
{
printf("%d ",n);
return;
}
printn(n-1);//printn(4)
printf("%d ",n);
}
// void printn(int n) // 4
// {
// if(n==1)
// {
// printf("%d",n);
// }
// printn(n-1);//pirntn(3)
// printf("%d ",n);
// }
// void printn(int n) // 3
// {
// if(n==1)
// {
// printf("%d",n);
// }
// printn(n-1);//pirntn(2)
// printf("%d ",n);
// }
// void printn(int n) // 2
// {
// if(n==1)
// {
// printf("%d ",n);
// }
// printn(n-1);//pirntn(1)
// printf("%d ",n);
// }
// void printn(int n) // 1
// {
// if(n==1)
// {
// printf("%d",n);
// return;
// }
// printn(n-1);//pirntn(4)
// printn("%d",n);
// }
int main(){
int n=5;
//scanf("%d",&n);
printn(n);
return 0;
}