Chapter 1 :Basics
1) Basic Printing, n wali cheez.
2) Variables, printing variables, int, float, and +,-,*,/ of integers.
3) Variables naming rules.
4) Comments
5) Taking Input
6) Modulus Operator
7) Float to int, int to float
8) Hierarchy
9) Char and ASCII
3.
Basic program inC
#include<stdio.h>
int main(){
printf(“hello world”);
return 0;
}
Cow is
white
VS Code
I
Low has y legs
Cow gives milk
cow is an animal
4.
How to movein next line?
Example :
printf(“Hello PW”);
printf(“Hello CW”);
Output will be :
Hello PWHello CW
5.
Use of escapesequence ‘n’
Example :
printf(“Hello PW”);
printf(“n”);
printf(“Hello CW”);
Output will be :
Hello PW
Hello CW
.
Variables and theirDeclaration
Let us focus on int data type as of now.
1) Variables as containers :
& Output
I ⑤
Y
I
25
-
9.
Printing Variables inC & Updation of Variables
int x = 5;
printf(“%d”,x);
x = 7;
printf(“%d”,x);
x = x + 6;
printf(“%d”,x);
x = x - 20;
printf(“%d”,x);
~ Output
v 10 ·
S
- S x =
x
*
2
·
10
-
~ 110
x =
S*2 ·
110
- I
S
v
·
5
v X ⑧
10
-
x
=
x-10S
W
value
I
v
x =
N 100;
-
d
u
old
x =
110-10 S = S
10.
Arithmetic operations onint data type
int x = 5;
int y = 2;
printf(“%d”,x+y);
printf(“%d”,x-y);
printf(“%d”,x*y);
printf(“%d”,x/y); // issue
5/2 =
2
7
3
10
11.
Increment - Decrementoperators
int x = 5;
x/+;
printf(“%d”,x);
x/-;
printf(“%d”,x);
/+x;
printf(“%d”,x);
–-x;
printf(“%d”,x);
-
T
- Output - -
X
~
·
E
·
S
*
6 6
~
-
-
~ Output
~ ·
S
- ·
6
- · 6
X =
$6 -
Variable Naming rules
1)Variables can start from an alphabet or underscore _ .
2) Special characters except _ are not allowed.
3) Some particular keywords are not allowed.
4) Commas or blanks are not allowed.
Auto 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
intx; intraghor; W
~ or $.
~ -
>a$
~
~
auto
=
18.
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
19.
Taking input //Let us take a simple example
int x;
printf(”Enter a numbern”);
scanf(“%d”,&x); // user will give ‘x’ a value.
int y = x*x;
printf(”square of number that you gave is %d”,y);
20.
Taking input //SUM of 2 given numbers
int x;
printf(”Enter first numbern”);
scanf(“%d”,&x); // user will give ‘x’ a value.
int y;
printf(”Enter second numbern”);
scanf(“%d”,&y); // user will give ‘y’ a value.
int sum = x+y;
printf(”sum of the numbers that you gave is %d”,sum);
21.
Modulus Operator
*
+, -
,S
I 00
-
5%2
=
1
↓
arithmetic
49%4 =
1 operators
100%6 =
Y
a%b =
remainder when is divided
by
'b'
1
d
divisor
dividend
Typecasting
Ques : Takeinteger ‘x’ as input and print half of
the number.
Ques : Take float ‘x’ as input and check if it is an
integer or not.
>Very Important
-
*
-'If Else' Band me solve pavenge
24.
Hierarchy of operators
inti = 2 * 3 / 4 ;
printf(“%d”,i);
Precedence AS
Brackets) D,
m) Ass
maths Programming
-
-
273/4
=
24
0.75
=
1.5 24
3/4 =
24 0 =
0
2
*
3/4
=
6/4
=
1.5 I
I 24 3/4 =
6/4
=
1
It
same Left to
Right
25.
char data type
charch = ‘a’;
float, char
↓
int.
f %%a
%d
A
various characters?''9' $'4' ·
ab
I
! 8
"
26.
ASCII values
char ch= ‘a’;
L
↳ int X
=
5;
Lit
ch
Each character has an ASCII value (which is an
integer)
27.
ASCII values
char ch= ‘a’;
yaad Jenni chahiye
a
=
97
'A=
65 10 =
48
I I
"B =
98 'B =
66 ' =49
Sh =
99
C =
67 I
v
1 ↳
↑
1
I
I
I
I
-
1
&
T
↳
13
=90 19 = 57
1
=
122 2
2
MCQ 1
Which ofthe following is NOT a character constant
(1) ‘Thank You’
(2) ‘Enter values of P, N, R’
(3) ‘23.56E-03’
(4) All the above
-
30.
MCQ 2
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
-
v
31.
MCQ 3
Which ofthe following statements is false
(1) Each new C instruction has to be written on a separate line
(2) Usually all C statements are entered in small case letters
(3) Blank spaces may be inserted between two words in a C statement
(4) Blank spaces cannot be inserted within a variable name
I
very emp
- FALSE
TRUE
TRUE
TRUE
32.
MCQ 4 (Homework)
Ifa is an integer variable, a = 5 / 2 ; will return a value
(1) 2.5
(2) 3
(3) 2
(4) 0
inta =
5/2:
printf/"%d", a);
~
MCQ 6
The expressionx = 4 + 2 % - 8 evaluates to
(1) -6
(2) 6
(3) 4
(4) None of the above
a%-b) =
aYob
a%b =
a(a<b]
x =
y +
2% -
8
~
-> 4t 2%8
-> n +
2
= 6
35.
MCQ 7
What willbe 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
float
d =
217;
-
d =
2.0/7;
~
- Enter anumber: 8
-
-
-
~
~
⑤ I
Positive Number
41.
v
v Enter anumber: -
y
-
- Negative Number
W
~
↳ I
X
42.
Ques : Takepositive integer input and tell if it
is even or odd ⑩
intx;
Scant (""od",92);
if (even) printf/"even No");
if (odd) printf/"Odd Number");
if (Yor2 ==1) even
-
if (condition) work;
d
time, false
HW : Takepositive integer input and tell if it is
divisible by 5 or not.
45.
Ques : Takeinteger input and print the
absolute value of that integer
mod
X =
Y 1x1 =
141 =
4
x
=
- 1 1x1 =
1 -
71 =
7
if (x =
=
0 e (x) =
x
it(x > =
0)
if (X <
0)
46.
Typecasting
Ques : Takeinteger ‘x’ as input and print half of
the number.
Ques : Take float ‘x’ as input and check if it is an
integer or not.
>Very Important
-
~
->
If Else'
47.
-
~ Enter areal number: 3.4
~
- Number is not integer
~
~ Tme
v
f
False
X a
I
-
~
↳ - ⑳
48.
Ques : Ifcost 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.
intsp;if (sp-p)
int cp;
Profit
sp-cp
49.
Else statement
i n
a
↳
.used alongside/after ifstatement
·
if your 'if'statement
fails, then else will zn.
↓
2 times check
50.
HW : Giventhe length and breadth of a rectangle,
write a program to find whether the area of the
rectangle is greater than its perimeter.
Ques : Takepositive integer input and tell if it
is a three digit number or not.
if (x > 9900 x <1000)
print->
53.
Ques : Takepositive integer input and tell if it
is divisible by 5 and 3.
d L
it(X%S
=
=
0) if(x%03
=
=
0)
b 6
if (x%3 = =
0 && n%3 =
=
0)
-
3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33..
5, 10, 15, 20, 25, 30, 3....
54.
Ques : Takepositive integer input and tell if it
is divisible by 5 or 3.
55.
Ques : Take3 numbers input and tell if they
can be the sides of a triangle.
a, b, c s sides of D
a +
b > c
b +
c > a
c +
a > b
if (a+b> && btc a && cacb)
56.
Difference blw &&and 11 operators
e n e e
if (condition 2 && condition 2) if it is false, then
true tie -> true
comfiler doesn't
even
true false
-
falle - check for the second condition
false
true
~
false
I
false falle -
false
if (condition 11 condition 2)
true tre ->
true
if it is
true, then
time falle -
true
I-> compiler doesn't even
falle time s true
check for the Ch.
false falle -
false
57.
Ques : Take3 positive integers input and print
the greatest of them.
A
Idistinct)
a, b, c
if(acbo& ax) -
if (bx &&b x
a) -
I only works for distinct
no.
if(caxb) -
58.
Ques : Take3 positive integers input and print
the least of them.
HW
=>
(Distinct)
59.
Ques : Anyyear is input through the keyboard.
Write a program to determine whether the year is a
leap year or not. 4
stu, 1900, 1700
366 days
~ x
X ~
~
2000, 1650
2000, 2023, 1900, 1600, 2040,2020
·
Leap year
occurs after every
4 years (but
·
Any year
divisible by 100 is
not a
leap year (but
·
Any year
divisible by
400 is 100% a
leap year
60.
HW : Takepositive integer input and tell if it is
divisible by 5 or 3 but not divisible by 15.
Ques : Takepositive integer input and tell if it
is divisible by 5 and 3.
x
=
3 - Not divisible by 5&3
x
=
3 - Not divisible by 5 and 3
x =
15 - Divisible by 5&3
63.
Ques : Takepositive integer input and tell if it
is divisible by 5 or 3 but not divisible by 15.
x
=
9: Condition valid
x
=
20: condition valid
x
=
45: Condition Invalid
x
=
1: Condition Invalid
64.
Ques : Take3 positive integers input and print
the greatest of them.
A
without
using (multiple conditions)
a, b, c
if (a < b) [
if (ax) - a is greatest
else
65.
HW : Ifthe ages of Ram, Shyam and Ajay are input
through the keyboard, write a program to
determine the youngest of the three.(using nested if else)
Ques : Takeinput 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
method-1 (Multile conditions) x =
90
Very Good
x =
45 Average
68.
Ques : Takeinput 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
Method-2 Else I5 x
=
90
Very Good
x =
70 Good
X
=
50 Average
x =
20 Fail
69.
HW : Givena 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). (Else-if)
Y
I I(+,+ 7
(-
x
+
)
X
-
# IV
-
(---) (+, -
3
70.
HW : Giventhe coordinates (a, b) of a center of a
circle and its radius, write a program which will
determine whether a user entered point lies inside
the circle, on the circle or outside the circle.
a,b, w x, y -
user input
I
given
x
2
+
y2
=
22
(X-a + (y-b)2 =
22 on the circle
(x-a+(y-b)2 >
22 - outside
(x-a)+ly-b)
2
" - inside
71.
Ques : Take3 positive integers input and print
the greatest of them.
A
switch ( integerexpression ) {
case constant 1 :
do this ;
case constant 2 :
do this ;
case constant 3 :
do this ;
default :
do this ;
}
Syntax =
e
-char
operator
↑
a op b 5* 3
1 d
operands
Ques : Writea 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. (done)
For Loop
for(int i= 1; i<10; i++){
// code
}
increment
condition
i
=
i +
1
initialisation ↑
Hello World a 10 baar
80.
Ques : Printhello world ‘n’ times. Take ‘n’ as
input from user Output
-
Hello
v U
Y
/
-
I
·
Enter n: 3
~
3 &
Hello
Steps:
·Hello
1) Check false, finish
4 -
3 2) Work
2
W
X 3) Increment
I
4) Repeat
81.
How for loopworks : the various parameters.
initialization;condition; increment
d -
Ex baar bar baar
82.
Ques : Printnumbers from 1 to 100
1 to 10
10 times hello world
G
look a variables i
Print
the value
of
'' & then
obviously
'
is going to
increment
83.
Ques : Printall the even numbers from 1 to 100
-
Output
-
2 Y 68 10 .... 98100
#At:If Else
this
look is
running 100 times
84.
Ques : Printall the even numbers from 1 to 100
Md-2:By changing the increment
i =
1,2,3,4,5,8
...
-> it+/i= i+1
↓
i =
10...98,100
So baar look calga
->
>
>
85.
HW : Printall the odd numbers from 1 to 100
d
M-T M-2
-
I
d ↳
increment change
if else
86.
Ques : Printthe table of 19. 19
Oteut:
19,38,57,76,95,.. 171, 190
38
57
M-1:divisible by 19 [1-190] 76
e
4,90 times 95
114
Mice 10 times
only
I133
152
171
209
190
i =
13837-0a/s13154
87.
HW : Printthe table of ‘n’. Here ‘n’ is a integer
which user will input.
88.
Ques : Displaythis AP - 1,3,5,7,9.. upto ‘n’
terms. 3
technically I was
*
i c
=
22-
1
for(inti= 1; Eii =
i+
2)[
printing the value
I printf("%dn"; il;
of ''
3
an =
a, + (n-1)d
an=
1 + (n-1).2
an
=
1 +
2n-2
↓
Me
an
=
22-1
89.
Ques : Displaythis AP - 1,3,5,7,9.. upto ‘n’
terms. Without
the nth term formula
-
I will only use' as the number of rounds for the look
90.
HW : Displaythis AP - 4,7,10,13,16.. upto ‘n’
terms.
91.
Ques : Displaythis GP - 1,2,4,8,16,32,.. upto ‘n’
terms. cook
code
->
I will run the times
Ques : WAPto check if a number is prime or
not. d
Any number inis prime if
write a
it has no
factors except
10 h
program
it
sent
if (n%i
=
=
0(
a
WAD to check if no is
composite or not.
n
=
24,i=
2,w2
Predict the output
main() {
int j ;
while ( j <= 10 ) {
printf ( "n%d", j ) ;
j = j + 1 ;
}
}
Output
j
~
W
I
=>
j +
+
;
W
I
-
Garbage Values
103.
Predict the output
main() {
int i = 1 ;
while ( i <= 10 ) ;
{
printf ( "n%d", i ) ;
i++ ;
}
}
-
-
a
lot
o
-
104.
Predict the output
main() {
int x = 1 ;
while ( x == 1 ) {
x = x - 1 ;
printf ( "n%d", x ) ;
}
}
Output
~ W
I
·
~
105.
Predict the output
main() {
int x = 1 ;
while ( x == 1 )
x = x - 1 ;
printf ( "n%d", x ) ;
}
- Ioutput
w
I
106.
Predict the output
main() {
int x = 4, y = 3, z ;
z = x-- -y ;
printf ( "n%d %d %d", x, y, z ) ;
}
Output
I ⑧
v
~
Y 3
X
y 2
I
· 33 1
X - - =>
post decrement
4=
x
-
1
107.
Predict the output
main() {
while ( 'a' < 'b' )
printf ( "nmalyalam is a palindrome" ) ;
}
9798
L
malayalam
S I
Infinite Loop
=
108.
Predict the output
main() {
int i ;
while ( i = 10 ) {
printf ( "n%d", i ) ;
i = i + 1 ;
}
}
-
:
v N
I
②
109.
HW : Predictthe output
main( ) {
int i = 10 ;
while ( i = 20 )
printf ( "nA computer buff!" ) ;
}
110.
Predict the output
main() {
int x = 4, y = 0, z ;
while ( x >= 0 ) {
x-- ;
y++ ;
if ( x == y )
continue ;
else
printf ( “n%d %d”, x, y ) ;
}
}
S
d Y
·Output
X B
~
2 · 3 I
W ⑧ 3
v
-
I·-1S
x
y 2
· O y
111.
HW : Predictthe output
main( ) {
int x = 4, y = 0, z ;
while ( x >= 0 ) {
if ( x == y )
break ;
else
printf ( “n%d %d”, x, y ) ;
x-- ;
y++ ;
}
}
Ques : WAPto count digits of a given number.
n
=
12458 Algorithm:
ne
↓ n count
5 digit no. 12458 ⑧
1245
I
#
Fit:continuously divide
124 2
n
by 10 untill it
12 3
reaches O.
I U
condition - (n> 0) / (n! =
0)
work -0;
increment ⑧ 5
decrement
count++;
114.
Ques : WAPto print sum of digits of a given
number.
n =
12345
Hint 1:Sum =
1+2+
3 + 4+ 5 =
5 +
4 +
3 +
2 +
1
Here
#Hint 2:If we want
the last
digit of a number 'n'e 2% 10
-
Algo n ed sum
-
-
1234 Y 0
123 3 U
12
2 7
I I 9
knatam
O 10
115.
HW : WAPto print sum of all the even digits of
a given number.
1
n
=
2413/2536
↓
Sum
=
2 +
4 +
2 +
6
Hw.2 WAP to print
the
product
of all digits of a
given
-
number
116.
Ques : WAPto print reverse of a given
number.
A
n =
1234 #
-1:
last digit
(n
=
4 32 1 in
=
n/10
-
r = ~*
10;
Algorithm :
n 2 Id
E
1234 O Y v
=
r +
ld;
123 Y 3
12 43 2
I 432 I
⑧ 4321 X
117.
HW : WAPto print the sum of given number
and its reverse.
118.
Ques : Printthe factorial of a given number ‘n’.
- 5) or 3
=
5x4x3x2x1
-
6! =
6xSX4x3x2X1
A 2?
=
2x1
=>n!
=
nxn-1xn-2xn-3....3x2x1
n!=
product
of 1,2,3,.
. .
. .
product:
1;
119.
HW : Printthe factorials of first ‘n’ numbers
n = S
↓
I
2
6
24
120
120.
HW : Writea program to print all the ASCII values
and their equivalent characters of 26 alphabets
using a while loop. Kafrital)
Output:(Typecasting)
e
65 A
66B
67 C
68D
:
902
121.
Auth to accessthe
sources
Dev
Too -> break the
authentication
-
en, locker storage
L
can open 2 mins to
upload
anywhere
list
3. 11.3
data frames to read excel file
offset
eek baai
Ques : Printthe given pattern
*****
*****
*****
Solid Rectangle
m =
no.
of rows/lines
n
=
no.
of columns, no.
of elements in each line
m=
2, n
=
3
2
3
Output
0 X X X
·
**&
i =
xR3 ⑧
i
=
Y ABM ANBY
124.
Ques : Printthe given pattern
****
****
****
****
Solid Square
L
Inpute'sas the side
of square
125.
Ques : Printthe given pattern
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
Number Square
n
=
Y n
=
3
forlinti=1; ic=
n; i+ +
K
I 2 3
I printf("%d", i);
1 2 3
3
I 23
1 2 3 4
n
=
4
forlinti=1;ic =
n;i +
+
)[
I printf!" *");
3
*** *
Ques : Printthe given pattern
A B C D
A B C D
A B C D
A B C D
Alphabet Square
26
n=
4 n
=
2
Hint:ASCII values
-
AB
1 2 3 656667A BC
AB
I I 23 I 656667 - A BC
1 2 3 656667 A
B C
1 - A 2632
2 - B
3+ c & so on
Ques : Printthe given pattern
*
**
***
****
Star Triangle
m =
4 n
=
3 n =
S
X I 2 345;
X & I *
** * 2
>***
4 x x x x
& S a x x x x
differenceb/w i
square &
triangle
130.
Ques : Printthe given pattern
1
1 2
1 2 3
1 2 3 4
Number Triangle
131.
HW : Printthe given pattern
A
A B
A B C
A B C D
Alphabet Triangle
E
nc
=
26
*HW : Printthe given pattern
1
A B
1 2 3
A B C D
1 2 3 4 5
Alphabet Triangle
n
=
3
#
Ant:Use (ib-elses
& Number mix
134.
Ques : Printthe given pattern
****
***
**
*
Star Triangle Ulta
n
=
4 I 2 3 Y -
j (j =
1;jc
=
n+
1 -
i;j +
t)
I X & X *
i =
1, j =
1to 4
2 x x x
i =
2,j
=
1 to 3
3 a x
i =
3,j=
1602
Y X
i
=
4; i =
1 to 1
↓ i +
j
=
n+ 1
I
6 j =
n
+
1 - i
135.
HW : Printthe given pattern
1 2 3 4
1 2 3
1 2
1
Number Triangle Ulta
111 I
222
&
3 3
Y
136.
HW : Printthe given pattern
Alphabet Triangle Ulta
A B C D
A B C
A B
A
A A A A
B B B
&
C C
D
137.
S: Odd NumberSquare
n
=
4 n
=
3
13 57
135
13 S 7
135
135 7 135
I 35 7
138.
Ques : Printthe given pattern
1
1 3
1 3 5
1 3 5 7
Odd Number Triangle
2
- S
H.W.
I 35 7
& I 3 S
I 3
I
139.
Homework : n
=
32x
E
-
2 Y S
24 6 2
2 Y
246 & 2 4 &
2
24 6
2
46
Even Number
even Number
even Number
triangle triangle Vlta
Square
140.
*Ques : Printthe given pattern
1
2 3
4 5 6
7 8 9 10
Floyd’s Triangle
n = Y
I 2 3 Y
n
=
2 n
=
5
I
I
I
2 2 3
2 3
3 U 36
Y 7
89 10
11 12 13 14 IS
Homework -
-
123 I 23
456
& 4 S
789 S
141.
*Ques : Printthe given pattern
1
0 1
1 0 1
0 1 0 1
0 & 1 Triangle
n
=
4 I 2 3 Y S -j
I I
if(i+; %2 =
=
0) print (1)
2 ⑧ I
else O
3 10 I
Y ⑧ 10 I
S I 0 I ! f
↓
i
142.
Ques : Printthe given pattern
*
*
*****
*
*
Star Plus
n is odd
n
=
S
- I 23 Y S j
- -
- -
- -
-
↑ # # # #
if (i =
=
mid 11j = =
mid) -x
-
2 #
# & ##
dee e#
- -
- -
3 X X X * X
- -
Y #
I
& ##
S #
A & # #
-
L
n =
S
=
+
1;
143.
HW : Printthe given pattern
******
* *
* *
******
Hollow Rectangle
123456
I
Conditions for a
2
- - -
' - - -
1) First row
2) Last Row
3) First
Col
4) Last Col
144.
HW : Printthe given pattern
* *
* *
*
* *
* *
Star Cross
12345;
7
- -
2 - - -
when s *
- - - -
3 - - -
=n+1
- - -
i
145.
*Ques : Printthe given pattern
*
**
***
****
Star Triangle Reverse
I 234 j n
=
4
--- ->
it (it; >
=
n+
1) x
2
- -
3
de
-
Y
i
Method-
1
-
146.
*Ques : Printthe given pattern
*
**
***
****
Star Triangle Reverse
method-2
-
Using 2 loops inside look
-
123 Y
123 Y 12 3 U
I
- -
I
- - -
3 1 x
2--
3 -
-
2 -
2
I
2
* *
3 -
I 3 *
*
*
Y
Y ~
O y *
*
* *
I
j < =
n-
i
in
147.
Ques : Printthe given pattern
1
1 2
1 2 3
1 2 3 4
Number Triangle Reverse
-
-
-
H. W &
---
--
-
148.
HW : Printthe given pattern
Alphabet Triangle Reverse
A
A B
A B C
A B C D
*Ques : Printthe given pattern
*
***
*****
*******
Star Pyramid
1234567
!---
I - -
I X
-
2 -
I
2 *
*
*
-- B ↑
-
3 * *
* * *
-
Y *
*
** * *
I
i jc zit-1
151.
*Ques : Printthe given pattern
*
***
*****
*******
Star Pyramid
n
=
3
I
- -
1
-
-
X
2
- -
2 - * * *
3
-
3 x * * * *
n
=
Y nspe n-1 and
nst+1 and
152.
HW : Printthe given pattern
Number Pyramid
1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
solve by
me
153.
HW : Printthe given pattern
Alphabet Pyramid
A
A B C
A B C D E
A B C D E F G
return type
- MOutput
-
I
-
·
Hi, I am
Raghav Garg
w
-
·
Hey, Good Morning
-
-
·
How are
you?
⑧
·Hi, I work at
PW
~
~ I return 0; ⑧
3
1, 6, 7, 8, 2, 3, 4, 5, 9, 10
Ques : Pascaltriangle -> h
=
S
I I
I 2 I
7 2 I
I 33 I I 33 I
I Y 6 4 I I 464 I
I S
I
10
I
I
10 S I
I
I
I S 10 10 S I
0!= 1, I!
=
I
179.
Ques : Pascaltriangle
O I 2 3 Y S O I 23 4S
O I
O ·Co
I I I I 'Co,
2 7 2 I
I 2 2202, 22
3 133 I 3
3
o 3 " s
Y I 464 I Y "o "C, YCz "C3 "Cy
S I S 10 10 S I S Co SC,2, , s
dia; -> col no
w
180.
Ques : Pascaltriangle Homework
-
- - - -
I
---- I
--I -2- I
1 -
3- 3- 1
-
1 -
4 -
6 -
4- 1
181.
Scope of variableLocal & Global
Ankat
S
-
⑰
7070
~ a
-
v
v W
I
~
-
70
~ G
-
182.
Formal parameters andActual Parameters
->
formal parameters FP AP
1 L
+ab
variables/ the ade
containers values
> Actual Parameters
183.
Ques : Swap2 numbers
w 7 Output
-
A
W M W Y
W
~ 7
w
-
X
y
~ 47
-
* x is changed now
permanently changed
I77
w
w
184.
Ques : Swap2 numbers int temp =
x;
A:Using extra dabba/variable
-
X =
yi
M-It:Without
using 34 variable y =
temp;
-
-
Sw(X,yS
+2
-
2
W
↓ - 6+ 2 -
2
6 +
2
6 I
Aim: X
y
x =
x +
y
y
=
x -
y
x
=
x -
y
185.
Pass by value& Pass by reference
W ↳ E W
Output
Input
~ 3
~ X
- B temp 7
-
~
- 37
1 3
3
I3 7
~ - E 37
W
-
X
y
W
--
*Pointers Output
v
d W6
a soo
~
↓
aS00
I
·
90
v X
2
=90
-
a 500 9700
W
*
-
deneference
operator
star operator
188.
*Pointers
7 · 3
W
39400 ·7
G
- W W
X 2
W
v
Update/Change
a 400 a S00
I
e
- -
189.
*Pointers -
swap 2numbers swapl
u 9408 9800 3
~
↳ W L 3
~
X
y
temo
a 100 &200
-
I
-
O &2
7 3
i
Output
w
~ A 37
⑬ ↳
e
~
X
y
e 9400 a 800
e
Reference
190.
State TRUE orFALSE :
1) The same variable names can be used in different
functions without any conflict. TRUE
191.
State TRUE orFALSE :
2) Every called function must contain a return
statement.
3) A function may contain more than one return
statements.
FALSE (exception is
void]
TRUE
d
but only one return statement
is actually executed
192.
State TRUE orFALSE :
4) Same names can be used for different functions
without any conflict. FALSE
L
C++
2 Java are OOD Languages
but
I is
not
193.
State TRUE orFALSE :
5) A function may be called more than once from any
other function TRUE
6) A function can call itself (except
main) TRVE
2 1
w X~ a X Output
W
X
W ·
Good Morning
W ~
⑧
1 O
~
- L j
A
Good Morning
↑⑧
-
-- ~
I
-
Comprehensively
a ·
DRY RUN
197.
Ques : Printn to 1 lusing recursion)
ForEx:n =
S I
Output
L
S
Y
3
2
I
198.
Ques : Print1 to n - kind
of harder than to 1
Output
#:Using extra
parameter ·I
A 3
Y 3 ·2
w X
&
3
⑳ X --X
W
I 2 3
↑ 3 3
IE
-
~ X ~
v ~
⑧ X ~ X
33 43
~ ⑧
199.
Ques : Print1 to n (after recursive call)
-> without
using the extra parameter out
-
~ 1
X ⑧ 0 X Enter n: 3
⑧
V ~
:I
↑ 3
-
I =
=
x
2
↑
-
2
u
-
&
1
I
- 2 v ·3
~ X
r
w
W
-
v 3
2
⑧
-
Ques : Printsum from 1 to n (Parameterised)
3. X
3 Y
ve a
7
output
- X
-
⑧
2 7
X ~
1 ⑨
Enter n:
4.
↳ 10
↑ Y O
~1
-
R 9
I
↑⑧
x
⑧
x
X
⑧ a
w
3 U
↳ ·O 10
~ 10
e 0v
- - X --
·X
7
-
o 4,0
~
202.
Ques : Printsum from 1 to n (Return type)
- void, intr -
no 1
Sum(5) =
5 + 4+
3 +
2 +
1
sum(s) =
S+ sum(n)
-
d
sum/4)
=
4 +
3 +
2 + 1
E
I
Sum(y) =
4 +
sum/3)
-
sym(3): 3+ 2+ 1 I
sum/3) =
3+ sum)
sum(2)
=
2+ 1
sym(2) =
2 +
sum)13
sum()
=
1 sym(1)=
I
203.
Ques : Printsum from 1 to n (Return type)
15 10
sym(s)=
5 + sum/4)
↑sumb)
Recursive tree /Radder
-
↑
3 +
su(2)
↑ I
2+ seem(1)
t
recursive
↑
formula 1
[in
=
n +
sum(n-1)
E
Sum(1) =
1
recurrence relation
204.
Ques : Makea function which calculates the
factorial of n using recursion.
n!=
nxt
3x241
5!
=
5x4x3 X2x
fact(1) =
1
fact(s) =
3x2x1
I
fact(0) =
1
fact(Y)
I
4 x 3x2x1
↳
fact(s) =
Sxfact(n)
↳
t
=
nx
fact (n-1)
205.
Ques : Makea function which calculates ‘a’
raised to the power ‘b’ using recursion.
b S 2" =
2x(2x2x2)
a 2 =
2x2x2x2X2
2" =
2x23
d b
a =
axaxaxa . . . .
a O
C
- a
2
S
I 32 I
b terms
I⑰
b
a =
a x (axaxaxa...)
⑰ b. 1 times
a
=
axaxaxa ↓
=
a
=
ax a
daxab d
if (b=
=
0) return
**
b)
=
a*Gower(a,b-1)
206.
*Multiple Calls
Ques :Write a function to calculate the nth
fibonacci number using recursion.
1 2 3 S 8 13 21 34 5S 89/44&soon
n =
1 2 3 Y S
· 8 9 10 11 12
fibo(z) =
fibo(6) +
fibo(s)
6 it
=
fiboln-1) +
fibo(n-2)
recurrence relation
base:If n =
=
1 or n = =
2 return 1
207.
Recursion Tree
m e
fibo(s)College Wallah
S ↓
3
I
-Final Playlists
fibo(U)
↓
-d Sibol
d C
Programming
I I ↓
fibo(z)+
fibo(z)
+
fibo(l) Recursion
↓
Fiboll
fibo(z) +
1
↑
208.
HW : Powerfunction (logarithmic)
↓
332 = 3 x 33 332 =
3x36
ab =
? 331 =
3x330 31 =
38 x
38
3
8
=
3 x 3
7
I
32
38 =
3x 34 6 steps
37
= 3 x 3
6 S
calls 34 =
32 x 32 calls
36 =
3 x 3
S
I
32 =
3x3
3
Y
=
3x34
I↑
↑
S
I I 1
3
=
3 x 3
4
3 =
I
3
3
- 3 x 3 3
=
3x,
3
2 =
3x3
I
I
⑧
3
=
3 x 3
What is anarray?
↓
It is can be said as a
list of items
int/float/char) bool
211.
Syntax and Declaration⑳
aname=
24,1,7,9,23;
G
intx(
data size
tybe
9/2
↓
elements of
away'x
212.
Accessing Elements ofArray
- -
-using square brackets
updation &
indexing
int air [4)= [10,20,30,403;
ari
too
-
printf )"%od", avr(23);
213.
Printing Output andTaking Input
↓
↳ intx i
I
If I have to
print
all the
elements
scant("%od",2x);
of an
away
Loops
->
If away is of size 'n' then the
indexing
will be from t
214.
intarr [] =[10,20,30,403;
and it
o arr (27 =
90
d
↓
nx; bytes
16 bytes
in
215.
Ques : Arethe following array declarations correct?
int a (25) ;
int size = 10, b[size] ;
int c = {0,1,2} ;
Wrong
->
int a [2s];
correct
--
wrong
int size =
10;
↓
int b(size];
int [1 =
50, 1,23;
w
intc(39 =
90,1,23; 2
216.
Ques : Whichelement of the array does this
expression reference?
num[4] nam ,
"
It element
from
start
217.
Memory Allocation inArrays
int a
=
10;
⑱
int b =
20; W
a D
C
intc
=
30;
↓
intaxr[3) =
210,20,303; and lo
I contiguous memory
allocation
218.
Passing Array toFunctions
void swaplintx, intyl
or
I y
3
-
int
main))2
I
int x =
4;
I
inty
=
8;
X
y
Swap (X, y);
3
219.
Passing Array toFunctions
For int, float, care pass by value pointeres pass by reference
Idifferent behavious
220.
Ques : Calculatethe sum of all the elements in the
given array.
intavr() =
2S, Y, , 9,63;
Sum
=
0; $ 910192S
Ques : Findthe maximum value out of all the elements
in the array.
-
-
01 2 > Y
intarr[s]=
(4, 8, 1,9,33; mx
=
Y;89
int mx =
awr (0];
-ebm
5e
Or
m
=
max(a,b);
223.
Ques : Findthe maximum value out of all the elements
in the array. ->
*
ch-2 :intarr[4) =
[4, 8, 1,63;
1 intmx=-1; ->
problem
Jyada Use hoti - int mx=
INT-MIN;
224.
Homework : Findthe minimum value out of all the
elements in the array.
↓
2 methods
d
intmn=
aro]; then
compare with Itonl
↳ intmm=
FNT-MAX;
-
225.
MCQ : Whatis the difference between the 5’s in these
two expressions?
int num[5] ;
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
esize
-> declaration
index to update, assign, initialize
- /index
226.
State TRUE orFALSE :
1. The array int num[26] has twenty-six elements.
2. The expression num[1] designates the first element in
the array
3. It is necessary to initialize the array at the time of
declaration.
4. The expression num[27] designates the
twenty-eighth element in the array.
True
FALSE
FALSE
intarr[S];
TRUE
227.
HW : Givenan integer n. Create an array containing
squares of all natural numbers till n and print the
elements of the array.
his
Hill)
as
228.
Ques : Givenan array of integers, change the value of
all odd indexed elements to its second multiple and
increment all even indexed value by 10.
I
E
isscintic
229.
Ques : Countthe number of elements in given array
greater than a given number x.
arr
=
[1, 2, 3, 4, 5, 6, 73; target
=
4
230.
HW : Findthe difference between the sum of elements
at even indices to the sum of elements at odd indices.
231.
Ques:
Search for anelement
in an
away
6
awr=[1,2,4,8, 16, 12,9,33 x =
8
E
bool -> true/false
110
d
bool flag: false;
232.
Ques : Findthe second largest element in the given
Array. -> distinct number
arr=
[4, 1,2,2,5,6,6,8,9,93
# Hint:2 times for look
-
-
imax,
smat
233.
Ques : Writea program to copy the contents of one
array into another in the reverse order.
01 2 3 4 5 6
-
n
=
7
arr=
21,2,3,4,5,6,73
brr[i]: arr[n-1-i]
01 2 3 4 5 6
bor =
27,6,5, 4, 3,2,13
brr[i]:
arr[j]
bir [0] =
7 =
ar [s] -> it j =
n-
1
M
brr[1] =
aur(s] I
n-1-i
E
=> -
brr[2)
=
arr[4]
234.
Ques : Writea program to reverse the array without
using any extra array.
arr= [1, 2, 3, 4, 5, 6,73
01 2 3 4 5 6
↳arr= 27,6, S S 4, 3, 2,13
i
j
int i =
0; while (ij,
int j=
n-1;
I
Swap (arr (i), arr (i))
i + +;
si--;
235.
Homework : Ifan array arr contains n elements, then
check if the given array is a palindrome or not.
-> racecar
->
malayalam
-> arr=
[1, 2, 3, 4, 3,2,13
arr= [1,2, 2,13
What and
Why?
So farwe 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.
int arr[r][c];
This is a 2D array where r depicts number of rows in matrix and c
depicts number of columns in the matrix.
int arr(u);
int arr [w][C];
238.
int arr[3][3] intair [2][2];
0 I
0 I 2
I 34
int aur [2][4]; arr [0][0] =
1
arr (0][1) =
2
I
arr (1390) =
3
-
arr (1][I]
=
Y
239.
2D arrays
can becalled as
away of 'aways'
-> [1,2,33
I [E1, 2, 33, 34,5,63,[7,8,933
-> [4,5,63 -
- [7,8,93
240.
Initialisation of a2-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[ ][3] = {12, 34, 56, 78, 91, 23 } ;
y rows 2 cols
↑
-This works
intarr[2][] =
[12,34,56,78,91,233 -
X
Ques : Writea program to store roll number and
marks obtained by 4 students side by side in a
matrix.
Raghav Harsh Urvi Sanket
76
88 82 -1
82 91 98
80
-> Raghav 76 80
int arr[4][2]
Harsh 88 82
Vervi 8291
Sanket 71 98
HW : Findthe sum of all elements in a given
matrix.
250.
HW : Findthe product of all elements in a
given matrix.
251.
HW : Givena 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). print
m
=
5 rows
(11,21) =
20,1)
(12,22) =
23,2)
Eit
n
=
4 scols
252.
Ques : Writea program to Print the transpose
of the matrix entered by the user.
Done
↳ column-wiseprinting
the
1
253.
Strings
Array - collectionof variables int avr(s];
d Char arr [4]=
int, char, float
d
char
arrays
arr:
S'va;','h,aiv's
Think…
Is the followingcode snippet correct?
char str[13] = “PhysicsWallah”;
I wrong, give sto a win size of 14
arrestr
261.
Accessing individual characters
charstr[] = “Physics Wallah”;
Predict the output?
(a) printf(“%c”, str[5]);
(b) printf(“%d”, str[9]);
01 23456789101112
- C
- ascir
value of b
Input and Outputof string
without loop
(puts &
gets /
d
using %s' format
specifies
264.
Can we usestring (character arrays) as
pointers?
char str[] = “PhysicsWallah”;
char *ptr = str;
265.
We get anotherway of initialising strings
char *ptr = “Physics Wallah”;
Note: Such direct initialisation using pointers results in a read-only
memory allocation of character arrays and hence, causes undefined
behaviour when we try to change the characters.
ptr[0] = ‘m’; Error!
char *ptr =“Physics Wallah”;
printf(“%sn”, ptr);
ptr = “College Wallah”;
printf(“%s”, ptr);
Works perfectly!
Interesting thing about such initialization
~
~
I
Physics Wallah"
nu
pr
↳
/I
College Wallah"
268.
Why does thishappen?
Pointers change the address to which they point after initialising a new
character array!
char *ptr = “Physics Wallah”;
printf(“Address 1: %pn”, ptr);
ptr = “College Wallah”;
printf(“Address 2: %p”, ptr);
Implement: Copy onestring to another
char s1[] = “Physics Wallah”;
char *s2 = s1;
// Let’s change in s1.
s1[0] = ‘M’;
printf(“%s”, s2);
271.
Making our taskseasy!
Useful functions for C already in standard library!
strlen(char *str) Returns the length of string
strcpy(char *s1, char *s2) Copies the contents of string s2 to string s1
strcat(char *s1, char *s2) Concat s1 string with s2 and stores the result in s1
strcmp(char *s1, char *s2) Compares the two strings
strncpy(char *s2, char *s1, int len)
Copy substring of size len starting from s1
character pointer into s2.
272.
How. Reverse agiven string
-
-
↓
0123456
char str)l:
"ragharlo";
↳
n
=
7
L
i=
0,j =
6
What are Structures?
structstudent{
int rno;
char grade;
float percent;
};
user defined data ty/za
-
dabbe int, float,car
I
student
Every student
has some common
properties
d
~no,
percent, grade
Ques : Createa structure type ‘book’ with name, price
and number of pages as its attributes ->
classwork
struct book [
char name (20];
float
price;
int
no of pages;
3;
Struct
book b1;
b1. price
=
487
.5
b1.no ofpages=
500;
strupy 1b1. name,"Secret Seven");
283.
Ques : Findthe error
struct emp{
int ecode;
struct emp e;
}
struct emp [ ecode
int ecode; 1
E 3; 2
·d
struct
empe;
struct
empe;
ecode
Al
I
2
284.
Ques : Createa structure type ‘Person’ with name,
salary and age as its attributes. Declare and initialize 2
variables for this. Print the name of first person and age
of the other.
285.
How are structureelements stored?
↓
Memory Allocation intx; s ybytes
1
Merent
are
e
blt 6; -y
bytes
charch-
2
bytes
8 bytes
migans
Features of structures
Copyingone structure variable to another of the
same type int x
=
4; -
⑰
L X
int
y =X;
Student
32:
S1
d
↓ 6
M
y
=
6 y
290.
Features of structures
Nestingone structure within another structure
Struct car [
supercar
char type [20]; I
carV+ extra
int price; ↓
float
topspeed; Chasis,spoiler
3;
macbook air-> touch id, haptic trackpad, airdrop
Macbook Pros air +
HDM1 +
SD card
291.
Ques :
maruti.engine.bolts =25 ;
Which of the following is True?
1. structure bolts is nested within structure engine
2. structure engine is nested within structure maruti
3. structure maruti is nested within structure engine
4. structure maruti is nested within structure bolts
-
True
True
F
legenday Po
I
Po
292.
Features of structures
Astructure variable can be passed to a function
->
passed by value
w
7/B 76
70.7
-
~ B
/B
IB
B
~
a
/'B
-
⑧
W
~
What are Unions?
unionstudent{
int rno;
char grade;
float percent;
};
->
structures only
-
space efficient ↓
d
but
with a
discipline
1 /A
d At a time only one
struct - 10 bytes attribute can be
union -y
bytes accessed &
infact,
stored