Lecture 6
Lecture 6
Lecture - 06
Test Data :
Input: Enter a Number: 25 78 23
Output: 78 is largest.
Test
est Data
ata :
Input: Enter a Number: 55 23 51
Output: 55 is largest.
Test Data :
Input: Enter a Number: 55 13 91
Output: 91 is largest.
Nested if-
if-else statement
4
Input: A, B, C
No Yes
A>B
No Yes No Yes
B>C A>C
1 #include <stdio.h>
2 int grade(int a,a int b,
b int c){
3 int max;
4 if(a > b){
5 if(a > c)
6 max = a;
7 else
8 max = c;
9 }else{
10 if(b > c) int main() {
11 max = b; int x,y;
12 else printf("Enter two numbers: ");
13 max = c; scanf("%d%d%d", &x, &y, &z);
14 } grade(x,y,z);
15 printf(“%d is largest
largest.”,
” ma
max);
) ret rn 0;
return 0
16 return 0; }
17 }
Switch Statement
6
Problem:
Write a program in C to read any digit, display
in the word.
Input : 4
Output : Four
How we think?
7
………….
………….
Input : 9
Output : Nine
Input : other than 0-
0-9
Output : Invalid
How it work?
8
switch(n
switch(
(n){
Input : 0 case : 0
Output : Zero printf(“Zero”);
printf (“Zero”);
Input
p :1 break;
Output : One case : 1
…………. printf(“One”);
printf (“One”);
…………. break;
………….
Input : 9 case : 9
Output : Nine printf(“Nine”);
printf (“Nine”);
Input : other than 0-
0-9 break;
p : Invalid
Output default :
printf(“Invalid”);
printf (“Invalid”);
}
Structure of switch statement
9
switch (expression)
{
case value1:
//Statements
b
break;k
case value 2:
//Statements
break;
……………
……………
case value n:
//Statements
break;
Default:
//Statements
}
Rules of Switch Statement
10
Logical
L i l OR.
OR True
T only l if If c = 5 and
d d = 2 then,
h expression
i
|| either one operand is true ((c==5) || (d>5)) equals to 1.
Logical NOT
NOT. True only If c = 5 then,
then expression !(c==5)
! if the operand is 0 equals to 0.
Logical Operators (AND)
12
Operator : AND
Symbol : &&
A eবং B di জেন ঐ পয্ােকটটা িনেয় আেসা।
A and B both bring that packet.
A B A B Result
0 0 0
0 1 0
1 0 0
1 1 1
Logical Operators
13
int a = 5,
5 b = 5,
5 c = 10
(a == b) && (c > b)
True && True
1 && 1
1
Program Codes
14
Operator : OR
Symbol :||
A aথবা B েকu eকজেন ঐ পয্ােকটটা িনেয় আেসা।
A or B anyone bring that packet.
A B A B Result
0 0 0
0 1 1
1 0 1
1 1 1
Logical Operators
17
int a = 5,
5 b = 5,5 c = 10
(a > b) | | (c > b)
False | | True
0 || 1
1
Program Codes
18
char x;
if((( >=‘A’)
if(((x ‘A’) && ((x <=‘Z’))
‘Z’)) | | ((x
(( >=‘a’)
‘ ’) && ((x <=‘z’)))
‘ ’)))
printf(“Alphabet.”);
else if((x
(( >= ‘0’)) && ((x <=‘9’)) ))
printf(“Digit.”);
else
printf(“special
printf( special character
character.”); );
Program Codes
19
char x;
if(( ==‘A’)
if((x ‘A’) | | (x
( == ‘a’)
‘ ’) | | (x
( == ‘E’) | | (x
( == ‘e’)
‘ ’) | | (x
(
== ‘I’) | | (x == ‘i’) | | (x == ‘O’) | | (x == ‘o’) | | (x ==
‘U’)) | | ((x == ‘u’))
))
printf(“vowel .”);
else
printf(“consonant.”);
Logical Operators (Not)
20
Operator : Not
Symbol :!
A Result
0 1
1 0
Logical Operators
21
int a = 5,
5 b = 5,5 c = 10
!((a > b) | | (c > b))
!( False | | True)
!(0 | | 1)
!1
0
Logical Operators
22