C in One Shot
Part - 2
Control Statements
IF - ELSE
Ques : Take positive integer input and tell if it
is even or odd n%2 -0
d
ut Output n is divisible by 2
S odd C n is even
6
even
even
2
Any No. which is
divisible
by 2 is called
even
out
Enter a number: 7
W
#
Else n + 6
if (n%2 = =
0) [
printf/"even Number");
3
if (n%2! = 0) <
print ("Odd Number");
3
Ques : Take positive integer input and tell if it
is divisible by 5 or not.
HW : Any year is input through the keyboard. Write
a program to determine whether the year is a leap
year or not. (Considering leap year occurs after
every 4 years)
classwork 2000 2004 2008 2012 2016 2020
- .
2022 2026
Ques : Take integer input and print the
absolute value of that integer
Negative, Positive, zero
7 - 7
-8 - g
-
450 -> U50
Output
-
- Enter a number: -
6
- The absolute value is 6
N
s
-
⑩ n =
- 64(-
1) = 6
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. Also determine how much profit he
made or loss he incurred.
int <p;
SP>CP (Probity
int sp;
scanf)) CP<SP (Loss]
↓
Go
on
ate
Enter cost price:10
10:10 X
IEnter selling price: 10
-> CP>,SP
Ques : Given the length and breadth of a rectangle,
write a program to find whether the area of the
rectangle is greater than its perimeter.
A =
lb
b P =
2(l +
b)
b
Multiple Conditions
Using && and ||
Ques : Take positive integer input and tell if it
is a three digit number or not.
n+99 and n<1000
G if (nc99 08 <1000)
S
1
code
3
Ques : Take positive integer input and tell if it
is divisible by 5 and 3.
H.W.
. .
-
H.W.
if (n%S =
=
0 && n%3 = =
0)
S
3
Is se divisible
Ques : Take positive integer input and tell if it
is divisible by 5 or 3.
3 5, 6, 9, 10, 12, 15, 18, 20, 21
↑
P operator
Ques : Take 3 positive integers input and print
the greatest of them.
A
Call 3 are distinct
a, b, c
T
it (acb or ascld-a
is greatest
printf()
3
if (b > a 02b >
c) 6 greatest
H.W. Take a
positive integers input & print the greatest
-
a, b, c, d it(acb28ax&&acd)
HW : If the ages of Ram, Shyam and Ajay are input
through the keyboard, write a program to
determine the youngest of the three.
Ques : Take 3 numbers input and tell if they
can be the sides of a triangle.
a, b, c
b
a
C
a +
b - c
b
+ c > a
a +
c>b
Nested If - Else
if else be ander
if
Ques : Take positive integer input and tell if it
is divisible by 5 or 3 but not divisible by 15.
int n;
if (n%S = = 0 11 n%3 = =
0)
3. 5, 6, 9, 10, 12, 18, 20, 21, 24, 27, 33
18 -
11
#5
concept of Heirarchy -
BODMAS -> B, 0, DM, AS
n%3 =
=
0 11 n%3 = = 0 && n%1S! = 0
L ↓ ↓
True I
True I False
False
True
(n%S = = 0 11n%3:= 0) && n%1!=0-be
True True -> True False
Ques : Take positive integer input and tell if it
is divisible by 5 and 3.
if (n%3 = =
0)S
if (n%3 = =
0) <
3
3
Conditions
Damable
itandthis
-
7 1) a
=
=
b
W 2)a+ b
G
OF;-Update
n3)acb
nac
=
b
n
=
=
2;
w sac =
b
↓
True, False
if(n True if (n
=
2)
3
Ques : Take 3 positive integers input and print
the greatest of them.
a, b, c
S 3 I
i(a-b)5
if (a > c)
a is greatest
else
s c is greatest
a,b,c 7
, 8, 9
HW : If the ages of Ram, Shyam and Ajay are input
through the keyboard, write a program to
determine the youngest of the three.
N
ested Loops se ->
Else If
t
o
s D, CP
Ques : Take input percentage of a student and
print the Grade according to marks:
1) 90-100 Excellent
2) 80-90 Very Good
3) 70-80 Good
4) 60-70 Can do better
5) 50-60 Average
6) 40-50 Below Average
7) <40 Fail
91
intn;
if (n >9)&&n<100)
d printf (Ene.. S
81
x if (n > 8) 80n(91)
71
E
61
E
S1
C
↳
Maths
Ques : Given three points (x1, y1), (x2, y2) and
(x3, y3), write a program to check if all the three
points fall on one straight line.
[x3,53)
doublenh,y1,n2,y2, n3, y3;
O
Can a
double ml =
(yz-
y1)/(x2-
x1);
CallB ↓
*
A ↓ Slope (BC)
->
yy2 =
m2
S lope(AB) 23 -
x2
C
o
me
if(m1 =
m2)
Ques : Given a point (x, y), write a program to find
out if it lies on the x-axis, y-axis or at the origin, viz.
(0, 0). y
if (lies on x-axis) ( ·
Cobb a-coordinate = 0
& & o · r
3 (a,0)
y-coordinate =
0
de if (lieony/ <
3
else[
3
Ternary Operator
expression 1 ? expression 2 : expression 3
a
cool Banne
MCQ Time !
Predict the
Output
. . .
. . .
-
Predict the output
main( ) {
int x = 10, y = 20 ;
if ( x == y ) ;
printf ( "n%d %d", x, y ) ;
}
Out
W ·
10 20
-
xy
-
↓
w
va
I
*
-
independent of the above
if 'statement
Predict the output
main( ) {
int a = 300, b, c ;
if ( a >= 400 )
b = 300 ;
c = 200 ;
printf ( "n%d %d", b, c ) ;
}
-
-
⑳
Wow
ontoone
v
uninitialized variable
a
v
ra
Predict the output
main( ) {
int x = 3, y = 5 ;
if ( x == 3 )
printf ( "n%d", x ) ;
else ;
printf ( "n%d", y ) ;
}
&Not
e
N
v
-
I
Predict the output
main( ) {
int x = 3 ;
float y = 3.0 ;
if ( x == y )
printf ( "nx and y are equal" ) ;
else
printf ( "nx and y are not equal" ) ;
}
Output
-
v
-So, mandy are equal
I
*Predict the output
main( ) {
int x = 3, y, z ;
y = x = 10 ;
z = x < 10 ;
printf ( "nx = %d y = %d z = %d", x, y, z ) ;
}
Output
⑬
NW O
-
/ W
y z
I
· n =
10y
= 10z =
0
v
=>
false - O
-
Bean: int n
=
3;
bame = tree print 1"ood", u =
=
10) ;
-
↓
false Not false
- O
int n =
3;
⑮
printf ("%d", x
=
10) ; N
HW : Predict the output
main( ) {
int a = 5, b, c ;
b = a = 15 ;
c = a < 15 ;
printf ( "na = %d b = %d c = %d", a, b, c ) ;
}
Predict the output
main( ) {
int k = 35 ;
printf ( "n%d %d %d", k == 35, k = 50, k > 40 ) ;
}
⑬
R
true - 1 thee1
Output
-
o
· 1501
HW : Predict the output
main( ) {
int x = 15 ;
printf ( "n%d %d %d", x != 15, x = 20, x < 30 ) ;
}
Predict the output
main( ) {
int i = 65 ;
char j = ‘A’ ;
if ( i == j )
printf ( “C is WOW” ) ;
else
printf( "C is a headache" ) ;
}
f
⑤
cwow
A
-
I
Output
v
v
HW : Predict the output
main( ) {
int a = 500, b, c ;
if ( a >= 400 )
b = 300 ;
c = 200 ;
printf ( "n%d %d", b, c ) ;
}
⑭
The Real Thing:
if ( condition )
statement ;
if ( expression )
statement ;
The Real Thing:
if ( 3 + 2 % 5 )
printf ( "This works" ) ;
if ( a = 10 )
printf ( "Even this works" ) ;
if ( -5 )
printf ( "Surprisingly even this works" ) ;
true
I -
if ( I any
no
except
o
E
/code-
any
character
3
3 + 2%5
↳
3
t = S
The Real Thing:
if ( condition )
statement ;
if ( expression )
statement ;