0% found this document useful (0 votes)
66 views234 pages

C++ in 10 Hours PDF

The document provides an introduction to basic C++ programming concepts such as variables, data types, arithmetic operations, taking user input, modulus operator, typecasting between integers and floats, and operator precedence. It includes examples of printing output, declaring and updating different variable types, and calculating simple programs to demonstrate concepts. Homework involves calculating the volume of a sphere.

Uploaded by

sharjosam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views234 pages

C++ in 10 Hours PDF

The document provides an introduction to basic C++ programming concepts such as variables, data types, arithmetic operations, taking user input, modulus operator, typecasting between integers and floats, and operator precedence. It includes examples of printing output, declaring and updating different variable types, and calculating simple programs to demonstrate concepts. Homework involves calculating the volume of a sphere.

Uploaded by

sharjosam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 234

11 Laptop/PC

2) Dedication

C++ Programming
in One Shot
Chapter 1 : Basics
1) Basic Printing, \n and endl wali cheez.
2) Variables, printing variables, int, float, and +,-,*,/ of integers.
3) Variable naming rules.
4) Comments
5) Taking Input
6) Modulus Operator
7) Float to int, int to float
8) Hierarchy
9) Char and ASCII
Basic program in C++ Output

Hello World
#include<iostream>

using namespace std;

int main(){

cout<<“hello world”;
}
How to move in next line?

Example :

cout<<”Hello PW”;

cout<<”Hello CW”;

Output will be :

Hello PWHello CW
'DRY RUN'

Out
Hello PW
·

a ⑧

-
- ·
Hello CW
~
Use of escape sequence endl and ‘\n’

Example :

cout<<”Hello PW”;

cout<<endl;

cout<<”Hello CW”;

Output will be :

Hello PW
Hello CW
*
Athing:

1) Cont << Y;

Cont<< "Y";
2)

cont 4+ 3;
3) <<

1) Cont < < "4+3";


Variables and their Declaration
-Boxer, Dabbe
Let us focus on int data type as of now. Output

I
1) Variables as containers : S

-
s
X

-
-
Printing Variables in C & Updation of Variables
Output
-int x = 5;
I

I
W
~ cout<<x<<endl;
·
S
/7
S
/
-x = 7; ·
I
X
rcout<<x<<endl; · 13

-
x = x + 6;
x x
= -
20 0 7
=

- cout<<x<<endl;
x 13
= - 20
vx = x - 20;
= -
7
~cout<<x<<endl;
-
-

-

X
Output
-

·S
-
08
tim
Arithmetic operations on int data type
Wint x = 5;

I
~int y = 2; Wk ·
7

x y -
3
Wcout<<x+y<<endl;
·

·
10
~cout<<x-y<<endl;
·

2
~cout<<x*y<<endl;

~cout<<x/y<<endl; </ issue

intlnt
Increment - Decrement operators - Thodi

#45
der me
~int x = 5;
⑤S Output

I
-x<+; -

X
-cout<<x<<endl; 6

-x<-; S

wcout<<x<<endl; 6
~<+x;
S
-cout<<x<<endl;
~–-x;
~ cout<<x<<endl;
Post Increment
put
v ·
Y

a
~
·U
v
-
·
S
X

-
Due increment
-

~
·4


~

~
·
S

~
X
&
S
Storing
float data type - Real Numbers

float x = 3.1;
Arithmetic operations on float data type
float x = 5;

float y = 2;

cout<<x+y<<endl;

cout<<x-y<<endl;

cout<<x*y<<endl;

cout<<x/y<<endl;
Example : Calculating Area of a Circle
- A=πr2
d

radius

area

3. 1415...
Example : Calculating Simple Interest

00
I
SI = PRT P
540
=

SA8. Y
-

100
2 32
=

t3 =

float p;
floatwi 32x3 S184
float ti
sup +

-
To
-

100
float si;
= 318.4
Homework : Calculate Volume of a Sphere
Variable Naming rules
-1) Variables can start from an alphabet or underscore _ or $ .
~2) Special characters except _ and $ are not allowed.
3)
~ Some particular keywords are not allowed.
~
4) Commas or blanks are not allowed.
a

Auto
E double int break extern enum unsigned while
case sizeof for const static long continue float
else signed do short switch char volatile default
goto struct if union return void register typedef
Variable Naming rules - Examples
Q. Which of the following are invalid variable names and why?

BASICSALARY _basic basic-hra

#MEAN group. 422

population in 2006 over time mindovermatter

FLOAT hELLO queue.

team’svictory Plot#3 2015_DDay


Taking input // Square of a Number
Take 2 numbers input from user and print their Sum
Modulus Operator 8 5/3

wea
-

aYob is remainder -

3 IS
d divided
②5403
is

b
+, -

,,1,% by

S 8
x
y
=
+

x
=

->

2
=

3 x
y
-

-y
IS
xxy
=

1
x/y
=

2
x%y =
Modulus Operator ->
importance
is
divisibility
2%8 2
Some impant pants:
=

i) aYob a
=
[it a<b>
0
Goa
=

2)

3) aY)-b) aYob =

4) ( a)%b
-
-
= (a%b]
Typecasting float tint,
-> inte float

Ques : Take integer ‘x’ as input and print


half of the number.
intx;

in x;
Hierarchy of operators a
very important (BOD MAS)

int i = 2 * 3 / 4 ; BY OP D, M/ A,S
L
cout<<i; Right
Left
to

#(int)

I
i =
mate,an i 2
=

3/4
*

6/4
=

2
=
*
0.75
=In
-D i 2
=

3/4
*

6/4 = 2 0
*

- 0X
=
char data type
char ch = ‘a’;

4 a, b, c... 2

2
A, B, C...

%, v, 2, 1,
1, 2, 4, 3, "is -st, =, 2,3, 11, 2
!D,H, $,

·
?, I,,., , w, 2

0,112, 3... 9
ASCII values -very important [Typecasting]
char ch = ‘a’;

65 ⑩8
297 + 66
B
11's 49

b + 98
67
1

C3
S

199 I

I
/

de 100 /
S

: '9'e57
!
i =
90

2 + 122
MCQ Time !
Homework
In b = 6.6 / a + 2 * n ; which operation will be performed first?
(1) 6.6 / a
(2) a + 2
(3) 2 * n
(4) Depends upon compiler
MCQ
Which of the following statements is false
(1) Each new C++ instruction has to be written on a separate line FALSE

(2) Usually all C++ statements are entered in small case letters TRUE

(3) Blank spaces may be inserted between two words in a C++


statement TRVE

(4) Blank spaces cannot be inserted within a variable name TRUE


C. W.

Homework float
-

f i
d
The expression, a = 7 / 22 * ( 3.14 + 2 ) * 3 / 5 ; evaluates to
: i
(1) 8.28
a 7/22*5.1443/5
=

(2) 6.28 i f
*
O
*
A
5.14 3/5
(3) 3.14
=
- a

~
(4) 0
=>) a - 043/S

=) a Is
=

- C 0
=
Ketant:

float x
st
=

d
5/2 2
=

int 2.S
3.0/2 =

W 5/2.0
3.0/2.0
2.5
=

2.S
=

-
real no

no accept
MCQ 1, 4,%7+,
-

The expression x = 4 + 2 % - 8 evaluates to


x 4 2 6
(1) -6
=
-> =
+

~
(2) 6 2% -
8 2%8
= 2
=

(3) 4

(4) None of the above

a%(b) ajob
=

a%ob a
=

[acb)
**MCQ -> Homework

What will be the value of d if d is a float after the operation


d = 2 / 7?
(1) 0

(2) 0.2857

(3) Cannot be determined

(4) None of the above


Chapter 2 : Conditionals
1) if, if - else
2) nested
3) Else if ladder
4) Ternary
5) switch
ans/output
situation dependent

IF - ELSE
I
5522 ad
Ques : Take positive integer input and tell if it
is even or odd even
d
int ni
0
Is
=

rm
cinn;
it
1) Cont "Even";
C
it 0
=
even

2) Cont "Odd"; FO odd

-> a&b
d

a
=

=
b,a! b
=
w Output

I
I W
3
Enter n: 3
n

I Odd number
HW : Take positive integer input and tell if it is
divisible by 5 or not.
Ques : Take integer input and print the
absolute value of that integer +, x, -, 1, % Ari
OP
int ni
7 ,,
==,! =,
plate
7 ->

cin;

10 - 10

cout<n
if (n >0) -
-
21 - 21

else ecout-h;
*Ques : If cost price and selling price of an item is
input through the keyboard, write a program to
determine whether the seller has made profit or
incurred loss or no profit no loss. Also determine
how much profit he made or loss he incurred.
int up; it(sp> <p) profit s
sp-cp

in cp;
It (cp sp)
< loss -

cp-sP
int sp;
if (<p sp)
=
=

to no

loss
cinsp; -
HW : Given the length and breadth of a rectangle,
write a program to find whether the area of the
rectangle is greater than its perimeter.

e,b
d

A l* b;
=

4 24(l b);
= +
Multiple Conditions
Using && and ||
and d
02
Ques : Take positive integer input and tell if it
is a three digit number or not.
if (n > 99 and n<1000) cont<< -

true
Ene
Ques : Take positive integer input and tell if it
is divisible by 5 and 3.
0 && n%3 0)
=

if (n%S
=
=
=
Ques : Take positive integer input and tell if it
is divisible by 5 or 3.

6, 9, 12, 15, 18, 21, 24, 27,30...


- 3,
39...
25, 30,
- 5, 10, 15, 20,

& 10, 12, 15, 18, 20, 21, 24, 25, 27, 31,
3, 5, 6, 9,

0 11 0)
1903 == -

if (4%5
=
=

n 22
=
Ques : Take 3 numbers input and tell if they
can be the sides of a triangle. 20, and

a, b, c
and 12+ ap b and (c+ a) > b)
if (19+ b) x

1a

b > c
a +

a
b c
=
+

a - b
c +
Ques : Take 3 positive integers input and print
the greatest of them.
a, b, c

b and sc) cont<< a..


it (as a

&& b, c) cont<< b
else if (b> a

else cont C
Homework : Take 3 positive integers input and
print the least of them.
HW : Take positive integer input and tell if it is
divisible by 5 or 3 but not divisible by 15.

4%3 0) and (n%/s!=0))


if ((n%s 0 =
=
=
or =
Nested If - Else
if ( 12
it
I
-

else-

Esses
I
3
Ques : Take 3 positive integers input and print
the greatest of them. withoutusing multiple conditions
HW : If the ages of Ram, Shyam and Ajay are input
through the keyboard, write a program to
determine the youngest of the three.
Else If
Ques : Take input percentage of a student and
print the Grade according to marks:

1) 81-100 Very Good


2) 61-80 Good
3) 41-60 Average
4) <=40 Fail
HW : Given a point (x, y), write a program to find
out if it lies in the 1st Quadrant, 2nd Quadrant, 3rd
Quadrant, 4th Quadrant, on the x-axis, y-axis or at
the origin, viz. (0, 0).

I
I
⑧ ⑧

⑳ ⑧

I IV
-
-



if-die cool banneke
but liye
d lines bachana

Ternary Operator
expression 1 ? expression 2 : expression 3 ⑨
replacementfor it-else
another
Switch Statement

useless
Syax: character int
I ↑
switch ( integer expression ) { n1, n2, op
case constant 1 :
do this ; int Char
case constant 2 :
do this ;
case constant 3 :
do this ;
default :
do this ;
}
Radar's
Recommendation
-
· exam se I
day before
Ques : Write a program to create a calculator that
performs basic arithmetic operations (add,
subtract, multiply and divide) using switch case
and functions. The calculator should input two
=
numbers and an operator from user.
int a;

int bi
charch;et,-, 1,
A

-> achb

n)
=>

op n2
MCQ Time !
Predict the output
Output

I
main( ) {
↳ 10 20

~int x = 10, y = 20 ;
~if ( x == y ) ;
cout<<x<<” “<<y ;
}
HW : Predict the output
int main( ) {
int x = 3, y = 5 ;
if ( x == 3 )
printf ( "\n%d",
-
= > x )I; (out xendl;

else ;
printf ( "\n%d",
= > y ) ; couty endl;
}
*Predict the output
out
- W

I
O
main( ) { 10
10 10 O
~int x = 3, y, z ; X y z

~ y = x = 10 ; (Right Left)
to

v z = x < 10 ;
0
↳ false -

cout<<x<<” ”<<y<<” “<<z ;


}
true->/
Chapter 3 : LOOPS ->
repetition

1) For Loop
2) Break and Continue
3) While and Do While Loop
4) Questions using Operators
5) Pattern Printing Problems
What and
Why?
baar hello world
For Loop condition int x 3;
=

↑ int i 1; =

for(int i = 1; i<10; i++){


1
declaration initilization
+

increment
// code decrement

-
Iterator
Ques : Print hello world ‘n’ times. Take ‘n’ as
input from user Done!
How for loop works : the various parameters.
n3 Initialization
=

Steps
-
-
"

1) Check condition

23 Work
Output

I
·
Hello World 3) Increment
Y
3
M Hello World

W
·

2 ·
Hello World
/
I
*

i
Ques : Print numbers from 1 to 100

using for look


Ques : Print all the even numbers from 1 to 100

it (1%2
=
0)
=
even

Method-2:
e
HW : Print all the odd numbers from 1 to 100
Ques : Print the table of 19.

171, 190
19, 38, 57, 76, 95, 114, 133, 152,
HW : Print the table of ‘n’. Here ‘n’ is a integer
which user will input.
Ques : Display this AP - 1,3,5,7,9.. upto ‘n’
terms. -
Method 1
-
! nth term formula
-

->
an a
= (n +
-
1)d

an
=>
1
=
+ (n-1)2

2n-2
an 1+
=
=>

an=
=>
27-1
4, 7, 10, 13....

Ques : Display this AP - 1,3,5,7,9.. upto ‘n’


-
-
-

rounds
terms. &
Method 2:Using extra
-
- variable &
keeping'only for the iterations
--
~
HW : Display this AP - 4,7,10,13,16.. upto ‘n’
terms. 3 4,d
=

a
=

an a
=
(n 1)d
+ -

an = 4 + (n -
1).3

4 3n +
-
3
an
=

3n+ 1
an
=

⑳34n 1
+
Ques : Display this GP - 1,2,4,8,16,32,.. upto ‘n’
urvo
terms. x2 x2 x2 x2
2x:
-> 5, 15, 4S, 135,

i = 1,ic n;i +)[


+

for lint
=

cont< a"
";
2;
+

a a =

3
HW : Display this GP - 3,12,48,.. upto ‘n’ terms.
Break;

thodi der me

he saath
ques
Ques : WAP to find the highest factor of a
number ‘n’ (other than n itself)
24 - 1, 2, 3, 4, 6, 8, 12,24

-
W

intf= 1; 1If me factor


i m;i + (
for
+

lint i =1; <

1 if (nii ==0) f i;
=

3
Ques : WAP to check if a number is composite
or not.
it n has extra factor/factors except 1 & then it is

composite
d

if (n%i 0)
== - we found a
factor
L

look -
- tnk
mitt
Ques : WAP to check if a number is prime or
not.
dane
Continue;
d
round
if you to
want skipa

print no. From 20
1 to except308
Hiw.

Ques : WAP to print odd numbers from 1 to


-

100. using continue


While Loop - An alternate to for look
I
Kitni baal weird scenario
chalega,
int i = 0; ↓
while(i<10){ multiple conditions
// code
i++;

}
Do- While
Loop -eK baar kam se kam zun kane

do {

//code

} while ( another == 'y' ) ;



Predict the output

I
int main( ) {
~int x = 1 ;

W
while ( x == 1 )
x=x-1;
cout<<x<<endl;
}
Predict the output
main( ) {
->
Infinite look
int i ;
while ( i = 10 ) {
cout<<i<<endl;
i=i+1;
}
}
Predict the output
main( ) { 1972983
while ( 'a' < 'b' )
cout<< “malayalam is a palindrome" <<endl ;
}
Infinite look
HW : Predict the output
main( ) {
int i = 10 ;
while ( i = 20 )
printf ( "\nA computer buff!" ) ;
}
HW : Predict the output
main( ) {
int x = 4, y = 0;
while ( x >= 0 ) {
x-- ;
y++ ;
if ( x == y )
continue ;
else
cout<<x<<” “<<y<<endl;
}
}
Questions using
Operators
+-*/ %
Ques : WAP to count digits of a given number.
5812
-> intn; n =

ein; int count

I
0;
=

While (n>0) E
#Hint:1)Integer division
-
n/10;
I
n
=
-

2) Divide in
by 10
again count+t;
& again 3
Ques : WAP to count digits of a given number.
n = 123 Y count 0
=

123 count-1
n =

12 count= 2
n =

1 count= 3
n
=

~
O
n
=
0 Ccount= 4

ans
finish
Ques : WAP to print sum of digits of a given
number.
n 1234
=
3
Sum 12 =
+
+ 4 =U
# Fit:1) Modulus (%) operator

2) 2%10 gives the last digit


1 2 3 y 4 3 2 1 +

3)
+
+ +
+ + =

n 512843
=
Ques : WAP to print sum of digits of a given
number. 1 3 y 4 3 1 2
+
+ +

ed n%10
=
+ 2
+ +
=

Algorithm: 4
e
1234 ed Y= Sum=
n =

7
n = 123 3

2 9
12

1 10
I


look
finished
HW : WAP to print product of digits of a given
number. (There won'tbe zero in the number)
a

For ex: n = 1234

12*3 *y 24
=
=

pro
HW : WAP to print sum of all the even digits of
a given number.
n 1234
=

2 +y 6
=

I
A
Ques : WAP to print reverse of a given
number.
n 1234
=
+ 5 4321
=

#Hint: 1) sum of digits


4321 0,0,4, 40, 43, 430, 432,4320,4321
2)
=
Ques : WAP to print reverse of a given
number. v

E 2 10
=

Algorithm
- 0 r r ld
153412/3 x
= +

n =

~ 0 44043
= - 430/4328
/432 - 4321

ed YBR 1=

1) Id
4
2) 2 10
=

3)2 + ld
=

4) n116
=
HW : WAP to print the sum of given number
and its reverse.
1234,2 4321
=

n =
Ques : Print the factorial of a given number ‘n’.
-
Find the sum
from in ent
↳ SX4x4X2x1
5!
=

8x 7 x6x5x4x3x2x1
8!
=

3 x2 x1
n! nxn-Dx(n-2)
....
=

02

1x 2x3x4....(n-1)xh
HW : Print the factorials of first ‘n’ numbers
d
n!
#Hint:
n 3,
=

I
11
=

2x1 2
=

2!
=

6
=3x2x1
=

4x3x2x1 24
=

4!
=

Sx4x3x2x 120
=

5!
=
HW : Write a program to print all the ASCII values
and their equivalent characters of 26 alphabets
using a while loop. 65
65 A 66

66B .
67C
I
90

:
90 Z
Chapter:4
-
nee

Pattern Printing
Questions
****
↓ ****
Nested Looks ****
d

Nested it-else
L

ek ke andar ek
Ques : Print the given pattern
n ↓
-
rectange
I *****
mxy ka star

***** 1

M ***** m
=

n=
6

* * *
* * X

& A & H Y A

Solid Rectangle
Ques : Print the given pattern

****
****
****
****

Solid Square
Ques : Print the given pattern
-n 4
=

n 3
=

1234 123

1234 123

1234 123

1234

Number Square
Homework: n S
- nY
=

n 2
= =

111

-ih
- 1 11

22
2222 2

33333

44n4y
SSSSS
Ques : Print the given pattern
n 4
=

n 3
=

ABCD A BC

ABCD
BC
A

ABCD
A BC

ABCD

Alphabet Square
HW:1 HW: 2
- -

a b c
A A A
A
b c
(n 3)
=

B BBB a

cc CC a b c

DDD D
Ques : Print the given pattern
I 23 Y -j
I
*
=
i 1,j 1 =

** E
1

2
jc
l
=

2,j
***
i 1,2
=
=

Y
**** i 3,j 1,2,3
=
=

I i 4,j 1,2,3,4
=

Star Triangle
Ques : Print the given pattern
-

H. W.

1
12 Number
123 -

Square - j = u
=

1234 code
jc i
d
=

Number Triangle
HW : Print the given pattern

A
AB
ABC
ABCD

Alphabet Triangle
I- row no.

I
HW:
- jecol no.

I ne no.
of rows

cols
22 m no. of
=

333 1
AX X 123 I
Y
yyy x X X 123 222

*** 123 333

↳ I

Seid
BC
A

ABC -

BC
A
*HW : Print the given pattern
n S
=

n3
=

1 I

AB A B

123 I 2 3

ABCD #Hint: If else

12345
-

Alphabet Triangle
↓ Ques : Print the given pattern
I I 234sj
I
**** i 1, j 1,2,3,4
=
=

*** itjmax
- n+1
2 =
2,j 1,2,3
=
=

**
i
3
i 3ej 1,2
=

i
=

*
1
Y Imax n+
-

=> =

i 4,1j 1
=
=

I *

*
-1- i

E
2*

jc 1
=

3 a* *

* * a
y *

Star Triangle Ulta


HW : Print the given pattern
H.W.
M.W.

1234 I 1 I I
Y

123
2
and 22 and 93
432
12
3 3

432 I

1 4

Number Triangle Ulta


HW : Print the given pattern

ABCD
ABC
AB
A

Alphabet Triangle Ulta


Ques : Print the given pattern
1 2 3 4 -
j
1
13579...
!
->
I
2
2
13 1 2 3
AP W

3 1 3 5 1 2 3 Y

1357 du
i
i jus

Odd Number Triangle


*Ques : Print the given pattern
I 3
24 ej
I
1
23 n 2
=

3
456 I

Y 7 8 9 10 23


n4
=

Floyd’s Triangle
*Ques : Print the given pattern
I 2 345+ j

1
when
I
0 1
(i+j7%2
01
=
=
-

2
-0
else
3 1 0 1

Y 0 1 0 1

S I 0 10 I

0 & 1 Triangle
Ques : Print the given pattern nodd
=

12345 -j

* it(i 311j 3) x
## ##
-
=
1 =
= =

2 ##
* #
#
else #

*****
-

*
## #
# n 3
=

S ##


* #
# A I
#

* **

I nS=

a #
#

Star Plus
HW : Print the given pattern
I 23456 -
j
******
I
first
* *
2
stars - now
- - -

i* *
-

-- last sow

d
****** firstcol
last
col
i

Hollow Rectangle
HW : Print the given pattern modd
* * - -

* * - -

* - -
-

* * --

* * - -

Star Cross
*Ques : Print the given pattern

I look me ander 2 looks
I
---
* I
I D

**
--

2 *
- -
2 2 X

***
-
-

3 - t
-
3 3 x x x

****
-

4 ax
y ya x

Star Triangle Reverse


HW : Print the given pattern

1
12
123
1234

Number Triangle Reverse


HW : Print the given pattern

A
AB
ABC
ABCD

Alphabet Triangle Reverse


HW : Print the given pattern
I X & & X

****
I
I
-
-

****
- - -

2 x x x &
2 --
t
3

****
--
-
x a x a
3
Y
-

****
-

Y x & & &


y &

Rhombus
*Ques : Print the given pattern

*
***
*****
*******

Star Pyramid
HW : Print the given pattern

1
123
12345
1234567

Number Pyramid
HW : Print the given pattern

A
ABC
ABCDE
ABCDE FG

Alphabet Pyramid
**Ques : Print the given pattern

*
Wallah Youtube Channel
Recommend !
College
***
-
d

***** playlists
******* d

*****
***
Course
c
Programming
* ↓

Lecture 4-PP
L

Badiya (Tough)
Star Diamond
Functions
What and DRY

as
Why? not repeat yourself
d

1) to tackle repetition
2) There are certainpeice of code that
we use a lot
of times
in
problem.
Basic syntax
fun(){

// code

}
Output

I
·Good
Morning
How are
you?
·

Hey
~-
·

function call Good


Morning
~ ·

~
How are
you?
·

of scall
main() -> It cannot be called more than once

return ;
Reyword
1 jisse function jaata
ka khatma ho

kind of like break;


Kaam ki baate :
1) main() ek hi baar aata hai.
2) Starts with main
3) unlimited functions
How functions work : ek ke andar doosra,
doosre ke andar teesra
Parameters:formal parameters/
Outat

I
-

7
↑ 90
dabbe

-
W7
W
90

a D

Fetual parameters
I
I

values

Pass value
by
Return type : Sum function se samajte hai
d
72 9
Out

Learn
I
be Sum a,
81 81
81
-

integer
take
Library functions
Squtt
<but()
min()
max()
ab
pow (a,b)
e
Ques : Combination and Permutation
me!
↓ ->
pr =

In,
2 -> >

"c a
=

a)(byc)
=

⑱x

( x!
-


3

*6xSx1 O
u!" -I =15
·:
=

y!x2! ⑰x1x2x1
Ques : Pascal triangle ~ For Look, Pattern
printing
D
O 12345 ej Nested for roofs
0 I

I 1 I ,
I 2 I I 2 I

↳ 133 I I 33 1

"
Y 146y I I Y

S 10 10 S I I is S I
S I

d
I
e;
Scope of variable

that
Formal parameters and Actual Parameters
d
I ↓
Done v
actual values

that
variables
are
dabbe
passed
Ques : Swap 2 numbers

MI:

X y temp

temp x
=

x
y
=

y temp
=
Ques : Swap 2 numbers
variable
M-2
-
-
using extra
without
-
Y

Lie
2 Y-
+

2 Y
+

X
2 My
X
y
X y

x x
y
+
=> =

y
x
-
=

y
=>

I x x
y
= -
Pass by value & Pass by reference
swap (x,3 Output
12:45

I
v
~
Y W 12 12 S

12 US

x y temp &

↳ ↳
~
45
-
- X
y
12 YS

Pass by value
*Pointers Y
-> bytes
int 2;
↳ E
=

int
I x
y

IIIIIIIIIIIIIII
ibyte
0x16....

*Pointers
- i
I
int x 3;
=

int* ptr 2 x;
=
x ptr
0x1I 0x14
coutyptrendl;

1 address in
is the
variable whose
Access the value of of the

pointer is
using dereference/I operator
*Pointers
Outfit

I
-

~ 3
W12 ⑲ -

23
~
X ptr
~
Ox1Y 0x26
~

Pass by Reference
*Pointers

I
Output
0x12 0x19

⑭ W
12 4S
v
&

in
- -

b
v 1/ *a 45
= a temp
- 0x22 0x44 0x 90

-
~
*
12
w
~~ US
w
~ X
y
⑧ 0x12 0x19

Pass by Reference
State TRUE or FALSE :

1) The same variable names can be used in different


functions without any conflict. YES!
State TRUE or FALSE :

avoid
2) Every called function must contain a return
statement. FALSE
3) A function may contain more than one return
statements. TRUE
State TRUE or FALSE :

5) A function may be called more than once from any


other function TRUE
Recursion
Function calling itself

1) Repetition
2) Infinite Loop
Ques : Print n to 1 ->
print
in no point

G
quintin-1

:
n S
=
s

w
-- Output

:I
↓ ↳
:3
2

~
- I

In
&

- I
-
~

-
condition
Base Case
=I
-
~ X out
-

3
:

o
·E
~ ·
&
~
v

2

-
S
Ques : Print 1 to n [Recursion]

contnendl;
I
2 print (n+1)

"
:
C
Ques : Print 1 to n (after
e recursive
n call)
~
Without extra Parameter
-
Proper Dry Pun

-
men Output

I
-
-

- "

· Enter n :3

W I
·

X
-
⑧ ⑧
a

! Is ·
3


X
3 - .


·
Ques : Print sum from 1 to n (Parameterised)
w
i -
College Wallah
I

Playlist

C
programming

Lecture - 6 Recursion

sum
N
1 to parameterised
Ques : Print sum from 1 toE
n (Return type)
n 5

+
->
=

1 23 4 5 15
=

-> + + +
+

5 4 3 2 1 +

1 2 3 4 3
=
+ + +
+
+ +
-> +

2 1
..3
+
+

sum(r) n
= +
=>

sum(n) n
= sumi-1)
+
ofmore
the
Ques : Make a function which calculates the
factorial of n using recursion.
5x4x3x2x1
3!
=

-> n! nxn-xn-2x....3x2x)
=

n! ux(n 1)!
-

- =

fact(n) n
=
fact(n-1)
x

fact(1) 1
=
Ques : Make a function which calculates the
factorial of n using recursion.
120
Tree
ener
Diagram :

fact( S C
2 24
3
S*fact (4)
=

↓46
4 x
fact(3)
22
factfat
3 *

i
Ques : Make a function which calculates ‘a’
raised to the power ‘b’ using recursion.
=3x3x3 x3
3
a 3
3"
=

81
=

I
b Y
=

-> a =
-
axa xqx a...
a

b times

1
ab
->
a
=
xab-

pow(a,b)
=>
a x
=

powla,b-1)
-> base case e b 0 =
ea:1
*Multiple Calls
Ques : Write a function to calculate the nth
fibonacci number using recursion.
1123381321345589 . .

9 10 11
n
12345678
=

fibo(8) fibo(z)
=
+

fibols)

-> fibo(n) fiboln-1) fiboln-2);


=
+

-> fibo() 1
=

fibo(2) 1
=
Gro
fibol
L

34
8
- ↑

3
·

L fibo(s) + fibo(y)

fibolus
L
Fiboli)
3 -
t
Hottub0(2)
2
1
·

fibr Fibulz Fibiesfibores finisfied


2
+

L
11
fibo(2) +
Sibol Enterthe
tree
HW : Power function (logarithmic)
L
⑳2
abz
I -
x

=
a

8 28 24x24
=

2 -
2 x 2

2" 22 22
=
x

27 2x26
22 2'xz
=

- =


6
2x2
2 -
2' 2x2
=

Y
2 - 2x2

z4 -
2x23
24 2x22
=

2x21
i
=

I 2 x 0
20
DSA e data structures &
algorithms
1

data stone - variable -> intx;

Arrays
What is an array?
d

I
11,76,13,88,46
List list
of integers
->
e

list char A, B, A, C, F
of is

50.S
list of float e 99.4,93.1,90.8,94.6,

dabbe band Santa noon.


el declaration se multiple
Syntax and Declaration -
Indexing
0 I 2 3 Y
int x(5];
X Y 2 6 -
2 10

x[0] 4
=
e
initializing of array elements (individual)

x[1] 2
=

x [4) 10
=

x(2) 6
=

x(3] = -
2
Accessing Elements of Array

↓ Very simple

brackets
using square
arr [41;
Printing Output and Taking Input

using looks
Ques : Are the following array declarations correct?

int a (25) ; X int a [25];

int size = 10, b[size] ; v

int c = {0,1,2} ; X intc(3) 20, 1, 23;


=

or

intc[1=20,1,23;

int size 10;


=

int b(size);
Ques : Which element of the array does this
expression reference?

num[4] int num [6];


d

5th element from start


4,
&

the index
4th element
Memory Allocation in Arrays

Elllllllllllllll
int arr[3]=24,3,23;
Passing Array to Functions

-
Reference
-
~

I
arrity
- Output
-

w
-
Ques : Calculate the sum of all the elements in the
given array. S
01 3 2 4

intavr[] [5, =
1, 2, 4, 6,33;

Sum 0;
=

G sum+=arr[0];

arr(1]
sum+=
&

&

sum+= arr[s];
Homework : Calculate the product of all the elements
in the given array.
Ques : Find the maximum value out of all the elements
in the array. 0 1 3 4 2

intarr[] [1,5,6,4,33;
=

intrx arr[O];
= mx = 56

mx max
=

(mx, arr(i])
Homework : Find the minimum value out of all the
elements in the array.
HW : What is the difference between the 5’s in these
two expressions?

int num[5] ;
unit,
num[5] = 11 ;

1. first is particular element, second is type


2. first is array size, second is particular element
3. first is particular element, second is array size
4. both specify array size
State TRUE or FALSE :

1. The array int num[26] has twenty-six elements. TRUE


2. The expression num[1] designates the first element in
the array FALSE
3. It is necessary to initialize the array at the time of
declaration. FALSE
4. The expression num[27] designates the
twenty-eighth element in the array. TRUE
HW : Given an integer n. Create an array containing
squares of all natural numbers till n and print the
elements of the array.
-> int ni n 4
=

cinn;

intains;
9/10
Homework;
Ques : Given an array of integers, change the value of
-

>

all odd indexed elements to its second multiple and


increment all even indexed value by 10.
01 2 3 Y

intarr(s) [1,2,3,4,53;
=

↳ ↳1, 4, 3,8,53

↳ [11, 4, 13,8,153
Homework
Ques : Count the number of elements in given array
--
-

greater than a given number x.


intarr[] 21,3, 0, 10,
=
2, 5, 63;
intx 4;
=

int count= 0;

if (arrlit,x) count++;
HW : Find the difference between the sum of elements
at even indices to the sum of elements at odd indices.
01 2 3 4 5 6

intaur)1=21,3,0,10,2,5,03;
9
sumeven=
sumOdd 18
=

↳ 9 - 18 -
Ques : Find the second largest element in the given
Array.
intarr[] < 10, 1, 0,9, 4, 12, 1,
=

23;
INT-MIN; my INN 1012
int
=

mx =

0; ic; it+ (2 smax:EMIN 10


for (int i =

I mx max(mx, awr [i]);


=

int smax: INT-MIN;


it+ d
for (int :=0:ic;
I if (aw(i)! mx) smax
=
=
max/Smax, aur[i]);
3
Ques : Write a program to copy the contents of one
array into another in the reverse order.

intads] 3 1,2,3,4,53;a
=
REALs
intb[S];
bit."
for (i 0;
=
ic 4;i
= +
+

)[
i -
i n 1- i;

E
=
-

j
i +
n -
1
[s];
=

b(i) a
=

3 E-i
Ques : Write a program to reverse the array without
using any extra array.
01 2 3 4 i 0;=

intal] [5, =
2, 3, 4, 13; j n
=
-

1;
i
I

# Lit 1) Swap - a [i], al;]

2) Two variables

in;
Homework : If an array arr contains n elements, then
check if the given array is a palindrome or not.

arr=[1,
2, 3, 4, 3, 2, 13 W

E 1,2,3,3,2,13
w
=>

[1,2,3,4,2,13
=
X
10 students
1) Basics

Else
Eddi
2) It E
P CM

3) Loope
4) PP

5) Function

6) Revision
& Pointers
2D Arrays
list
2) Arrays Array -

Arrays 2D

Away
Table
What and
Why?
So far we have explored arrays with only one dimension. It is also
possible for arrays to have two or more dimensions. The two
dimensional array is also called a matrix. grid
ecolumns
int arr[r][c];
↳ras

This is a 2D array where r depicts number of rows in matrix and c


depicts number of columns in the matrix.
declaration.
e

↳ int arr[4][3]; int als): ,"I


alz] 3
=

O 12

2 11 9 ->
ar(2]/2): 9

3 20


awe arr[3][T 20
=

arr[2]/0]=11
int an [2][3];
O I 2 -j 612 64
- 13
O 6 I 2 435
G

I Y 35 2S

[0][0] (1](0) Y
=

an
6
=
arr

arr[1] [11: 3
(OIC1) 1
=

aur

[0](2):2 arr[1] (2):3


arr
Initialisation of a 2-Dimensional
Array
int arr[4][2] = { { 1234, 56 }, { 1256, 43 }, { 1434, 32 }, { 1312, 96 } } ;

int arr[4][2] = { 1234, 56 , 1256, 43 , 1434, 32 , 1312, 96} ;

int arr[2][3] = { 12, 34, 56, 78, 91, 23 } ;

int arr[2][3] = {12, 34, 56, 78, 91, 23 } ;

2D
array -
array of away
intarr[333] =
[[1,2,33, (4,5,63,[7,8,933;
O 12

O I 2 3

I 436

2 I 89

aww

intaur (3[33:(91,2,33,44,5,63,97,8,933;

int avr[][3]=[1,2,3,4,5,6,7,8,93;
Ques : Write a program to store roll number and
marks obtained by 4 students side by side in a
matrix. Harsh, Sanket, Urvi
4 students -
Ragnar,
13 82 88
76
76 91 90
0 I 8 I

R O 16 81 2 3
0 I

H I 76 0 7613 82 88
13

81 90

to 90
S 2 82 al I

3 88 90
U R U
Ques : Write a program to store 10 at every index of
a 2D matrix with 5 rows and 5 columns.
int aur [S][S];
0 12 3 Y

""
10 18

is
co

2 10 10 10 10 10

3 10 10 10 10
10

10 10 10 10 10
Y
Homework
Ques : Write a program to add two matrices.
- -
-

0 I 2
0 I 2 0 12

O 9 4 2 O 2 I 6 O 11 S S
t I

I O I 7 I 3 9 Y I S 10 11

b C
a

cliJi):a(i][] b(i][i];
+

->
Ques : Find the maximum element in a given
matrix. ↓
ID
Ditto Same as
away

int mx INT-MIN;
=

busing traverse
nested look the entire ID
array
L

mx max
=
(mx, arr(i)(i));
HW : Find the minimum element in a given
matrix.
HW : Find the sum of all elements in a given
matrix. d
int sum 0;
=

Sum+= arr[i];];
HW : Find the product of all elements in a
given matrix.
int product: 1;

product x aur(i)(i);
=
HW : Given a matrix ‘a’ of dimension n x m and 2
coordinates (l1, r1) and (l2, r2). Return the sum of
the rectangle from (l1,r1) to (l2, r2).
O I 2 3 -j (11) and (4, 2)
O I 2 3 ↳
I 9 10 11
S
I
2 13 16
12
i 1 to Y
=

3 7
13 17 19

4 8 17 18 20

I -j 1 to 2
=

int a[8][4];
Ques : Write a program to Print the transpose
of the matrix entered by the user. AW create ->

1
d the

done transpose
Already
store it

Column Wise
Prin
ting in a new

O I matrix
O I 2

3 0 I Y
O 7 2

->
S 6 2 S
I Y I

2x3
2
3 6

3x2
2) Vector,2D vector

I
2) DS A
L

Time & space complexity

Sorting, searching
Linked list, stack, Quene, Trees, maps, Set, leaps

Graphs, DP, OOPS


DW
skills Sanket
Decode 2.0 Batch - C++, DSA (Paid) Mrvi,
eC++&DSAfoundation course
PWskills &
College Wallah -
Playlist
1D Array raghar
I I

integer away
char
array (6):

float, Char E','hav

Strings
!
What are strings -> Char Arrays

and Why are


they used?
L
questions
string str;

Declaration of Strings ~
strings:"Raghav Garg",

and taking Input



d

string si

cins;
getline/cin,s);
cout<<S;
Indexing of

I
length)).
characters in strings:"abc";
Strings
d sitic
strings:"raghar";
"o'-hull character
cont<S;
d
S90]://v ASCII - O
cont <<
Ques : Input a string -
of length
> n and count all
the vowels in the given string.
Updation of a
single character
in string

Done
H.W.
Ques : Input a string ⑦@°=I>
-
-
-
of size n and Update all
the even positions in the string to character
‘a’. Consider 0-based indexing.
012345678910

strings:"Raghav Garg";
aaahavaGara
E
Built-in string functions
d
STL

standard template library


size() / length()
d
baat kar le hai
push_back() & append()

strings:"raghar";
pop_back() & clear()

string to empty string


“+” operator
reverse()

strings:"raghar";
Ques : Input a string of even length and
reverse the first half of the string.
i I
012345

-> string s = "raghar";


-

↳ garhav
Homework
Ques : Input a string of length greater than 5 and
-
-
-

reverse the substring from position 2 to 5 using


inbuilt functions.

strings:"raghar";
012345


reverse (s. begin))+2, s. begin() +

1);
6
to_string() -> int to
string
d

int x 12345;
=

4
strings:to-string(x);
Ques : Return the total number of digits in a
number without using any loop.

Hint : Try using inbuilt to_string() function.


x 91632
=
stoi
d

string = "1412361";

intx stoils);
=
Maza aa
gay a

Thank you!

You might also like