CPP Notes Upto Recursion
CPP Notes Upto Recursion
**********************
what is computer?
electronic machine takes input and process it gives output
binary language
0 and 1
language
hindi grammer
english grammer
french
....
..
c language garmmer
progarm -> .c -> .o ->.exe(machine langauge) 01010100001101
...
10000+
Histroy of C language
ide-----code editor attachhed compiler
console -Black screen
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
& ^ % ! ( ) { } _ + =[] < ; > :~? - " "
''-character
""-string
**********************************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,
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
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:-
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()
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
3+4*5 = 23 by bodmas 4* ,4
+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));
3-4+5 = 4 by bodmas
3+1 =4
-1 + 5 =4 by c lang. rule
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==
= equal assign
4>5 - false 0
x=2;
x==3 - false
!true = false
!false = true
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
= (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?
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
1 byte = 8 bit
~00010101
11101010
Right shift
00010101>>2====00000101
left shift
00010101<<3=====10101000
main(){
code
if(condition)
{
code
code
}
code
}
#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");
}
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;
#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
{
return 0;
}
OUtput:Enter a number: 5
enter a second num: 6
divisible by 5
div by 2
int main(){
int i=5;
while(i<5){
printf("coding club");
i++;
}
}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(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;
}
lb2:
code
code
goto lb1;
code
code
lb1:
code
code
goto lb2:
if(condition==true)
{
code
goto a;
}
else
{
a:
code
}
/*
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
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);
}
#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>>
Write a code for addition like sum of two numbers which in the type (TSRN)
>>>>Solution:-
#include <algorithm>
#include <iostream>
#include <vector>
add(4,6);
return 0;
}
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>
***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
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);
}
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;
}