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
even
6
even
2
Any No. which divisible
is
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
SP>CP (Probity
<p;
int sp;
scanf)) CP<SP (Loss]
↓
I
Go
ate
on
Enter cost price:10
Enter selling price: 10
10:10 X
-> 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
1code
3
H.W.
Ques : Take positive integer input and tell if it
. .
is divisible by 5 and 3.
H.W.
&& n%3 0)
if (n%S
= 0
= = =
Is se divisible
Ques : Take positive integer input and tell if it
is divisible by 5 or 3.
21
3 5, 6, 9, 10, 12, 15, 18, 20,
P
↑
operator
Ques : Take 3 positive integers input and print
A
the greatest of them. Call 3 distinct are
a, b, c
(acb ascld-a
is greatest
T
it or
printf()
3
02b >
c) 6 greatest
if (b > a
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
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
BODMAS B, 0, DM, AS
concept of Heirarchy -
->
n%3 = 0
=
11 n%3 = = 0 && n%1S! = 0
↓ ↓
L
I False
I
True
True
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
Conditions
itandthis W
Damable
-
7 1) a
=
= b
2)a+ b
G n3)acb
OF;-Update
nac b
=
n
=
2;
sac b
=
w
↓
True, False
if (n 2)
(n
if
=
True
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
to
s D, CP
Ques : Take input percentage of a student and
print the Grade according to marks:
intn;
91 if (n >9)&&n<100)
1) 90-100
d
81
Excellent printf (Ene.. S
2) 80-90
x
71
Very Good if (n > 8) 80n(91)
3) 70-80
E
61
Good
4) 60-70
E
S1
Can do better
5) 50-60
C Average
6) 40-50 Below Average
7) <40 Fail
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.
doublenh,y1,n2,y2, n3, y3;
[x3,53)
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 x-axis) (
on Cobb a-coordinate 0
·
=
& & o · r
3 (a,0)
y-coordinate 0
=
de if (lieony/ <
3
else[
3
cool Banne
a
Ternary Operator
expression 1 ? expression 2 : expression 3
Predict the Output
MCQ Time !
.
-
.
.
.
.
Predict the output Out
I
*
main( ) {
-int x = 10, y = 20 ;
W ·
10 20
xy
-if ( x == y ) ; False the statement, but semicolon is used. So, this has no
effect on code. By using Semicolon, the statement is finished.
-
printf ( "\n%d %d", x, y ) ;
va
w
} ↓
above
independent of the
if 'statement
onto
Predict the output
main( ) {
v
int a = 300, b, c ;
-
if ( a >= 400 )
⑳
Wow one
b = 300 ;
uninitialized variable
-c = 200 ;
a
vprintf ( "\n%d %d", b, c ) ;
ra
}
Garbage Value, because it is in if block.
Predict the output e
&Not
I
main( ) {
vint x = 3, y = 5 ;
Nif ( x == 3 )
printf ( "\n%d", x ) ;
else ; This printf statement is not part of the else
block. It is always executed because it is
-printf ( "\n%d", y ) ; outside the if-else structure. Since there is no
block of code wrapped in curly braces {}, this
}
printf will always execute regardless of the if
condition.
This prints the value of y, which is 5
Predict the output Output
-
I
-So,
main( ) {
vint x=3; mandy are equal
float y = 3.0 ;
if ( x == y )
printf ( "\nx and y are equal" ) ;
else
printf ( "\nx and y are not equal" ) ;
}
*Predict the output
Output
NW
-
main( ) { ⑬
I
O
/ int x = 3, y, z ; W
y 10y
= 10z 0
=
z · n =
vy = x = 10 ;
O
false -
z = x < 10 ;
=>
printf ( "\nx = %d y = %d z = %d", x, y, z ) ;
}
-
Bean: int n 3;
=
tre e
print 1"ood", 10) ;
=
u =
bame = -
↓
false
- O
Not false
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 ;
⑬
R
printf ( "\n%d %d %d", k == 35, k = 50, k > 40 ) ;
} true - 1 thee1
Output
-
· 1501
HW : Predict the output
main( ) {
int x = 15 ;
printf ( "\n%d %d %d", x != 15, x = 20, x < 30 ) ;
}
A
I
Predict the output Output
main( ) {
⑤
f
cwow
-int i = 65 ;
vchar j = ‘A’ ;
vif ( i == j )
printf ( “C is WOW” ) ;
else
printf( "C is a headache" ) ;
}
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: true
I - no
excepto
if ( I any
if ( 3 + 2 % 5 ) E character
any
/code-
printf ( "This works" ) ;
3
if ( a = 10 )
printf ( "Even this works" ) ;
if ( -5 )
printf ( "Surprisingly even this works" ) ;
3 + 2%5
3t = S
The Real Thing:
if ( condition )
statement ;
if ( expression )
statement ;