C Programming | CCIT
1
C Programming | CCIT
Table of Contents
Conditional statements ......................................... 3
If statement .......................................................... 3
Boolean expressions ............................................. 3
Nested if ............................................................. 10
Ladder if statement ............................................ 14
2
C Programming | CCIT
Conditional statements
These are statements within our program which are conditionally executed.
To conditionally execute statements C Language provides us different control
structures and operators.
• if statement
• switch statement
• Ternary/conditional operator
If statement
It is used to conditionally execute a block of
statements
• If condition is true then statements within if
block are executed and then program control is
transferred to next statements.
• If condition is false then statements within else
block are executed and then program control is
transferred to next statements.
Note: else block is optional
Boolean expressions
• Condition can be specified by using a Boolean expression .i.e. an expression
whose result is true or false.
o true – 1
o false - 0
• Boolean expression can be created by using relational and logical operators.
o Relational Operators: < > <= >= == !=
o Logical Operators: && || !
and or not
3
C Programming | CCIT
WAP to read a no and check if it is an even no or odd no.
#include<stdio.h> .
Enter a Number 26
main() Number is Even
{
int a;
printf("Enter a Number ");
scanf("%d",&a);
if( )
printf("Number is EVEN");
else
printf("Number is Odd");
}
WAP to read 3 angles and check if triangle can be formed or not.
#include<stdio.h> .Enter 3 Angles 60 60 60
main()
Tirangle Can be formed
{
int a,b,c;
printf("Enter 3 Angles ");
scanf("%d %d %d",&a,&b,&c);
if( a+b+c == 180 )
printf("Triangle can be formed");
else
printf("Triangle cannot be formed");
}
4
C Programming | CCIT
WAP to read a no and check if it is a 2 digit no or not.
#include<stdio.h> .Enter a Number 26
main() It is a 2 digit Number
{
int a;
printf("Enter a Number ");
scanf("%d",&a);
if( a >= 10 && a <= 99 )
printf("It is a 2 digit number");
else
printf("It is not a 2 digit number");
}
WAP to read a no and check if it is a 4 digit no or not.
#include<stdio.h> .Enter a Number 2656
main() It is a 4-digit number
{
int a;
printf("Enter a Number ");
scanf("%d",&a);
if( a >= 1000 && a <= 9999 )
printf("It is a 4 digit number");
else
printf("It is not a 4 digit number");
}
5
C Programming | CCIT
WAP to read a no and check if it is a 4 digit no or not. WAP to read a no and check
if it is a 4 digit no or not. If it is a 4 digit no then find sum of its digits
#include<stdio.h> .Enter a Number 2656
main() It is a 4-digit no.
{ sum of number is 19
int a;
int x,y,z,w,s;
printf("Enter a Number ");
scanf("%d",&a);
if(a>=1000 && a<=9999)
{
printf("It is a 4 digit number");
w=a % 10;
x=a / 10 % 10;
y=a / 100 % 10;
z=a / 1000;
s=w + x + y + z;
printf("sum of digits is %d",s);
}
else
printf("It is not a 4 digit number");
}
6
C Programming | CCIT
WAP to read a no and check if it is a 3 digit no or not. If it is a 3 digit no then find
sum of its digits.
#include<stdio.h> Enter a Number 265
main() .It is a 3-digit no.
{
int a;
int x,y,z,s;
printf("Enter a Number ");
scanf("%d",&a);
if(a>=100 && a<=999)
{
printf("It is a 3 digit number");
x=a % 10;
y=a / 10 % 10;
z=a / 100 ;
s=x + y + z;
printf("sum of digits is %d",s);
}
else
printf("It is not a 3 digit number");
}
7
C Programming | CCIT
WAP to read marks for 5 subjects and check if student is pass or fail. Passing marks
40 per subject
#include<stdio.h>
main()
{
int a,b,c,d,e;
printf("Enter Marks for 5 subjects ");
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
if(a>=40 && b>=40 && c>=40 && d>=40 && e>=40 )
printf("Student is Passed");
else
printf("Student is Failed");
}
Enter marks of 5 subject 65 85 62 72 39
Student is Passed
8
C Programming | CCIT
WAP to read marks for 5 subjects and print total marks also print percentage if
student is pass.
#include<stdio.h>
main()
{
int a,b,c,d,e,t;
float p;
printf("Enter Marks for 5 subjects ");
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
t=a+b+c+d+e;
printf("Total is %d",t);
if(a>=40 && b>=40 && c>=40 && d>=40 && e>=40 )
{
printf("\nStudent is Passed");
p=t*100/500.0;
printf("\nPercentage is %f",p);
}
else
printf("\nStudent is Failed");
}
Enter marks of 5 subject 65 85 62 72 39
Student is Passed
Total is 342
percentage is 68.4
9
C Programming | CCIT
Nested if
• If a if statement is used within if statement then
such a control structure is called as nested if.
• Nested If statement is used to check a condition
only if another condition is true
WAP to read a no and check if it is a 4 digit no or not.
#include<stdio.h> Enter a Number 26
main() It is a 2 digit Number
{
Both digits are not
int a; same
int x , y ;
printf("Enter a Number ") ;
scanf("%d",&a) ;
if(a>=10 && a<=99)
{
printf("It is a 2 digit number") ;
x =a%10;
y =a/10;
if(x==y)
printf("Both digits are same") ;
else
printf("Both digits are not same") ;
}
else
printf("It is not a 2 digit number") ;
}
10
C Programming | CCIT
WAP to read a number and check if it is a 4 digit no. or not. If it is a 4 digit no. then
check if sum of its first 2 digits is equals to sum of its last 2 digits .
#include<stdio.h> Enter a Number 2653
.
main() It is a 4 digit Number
{ Sum is same
int a;
int n1,n2,n3,n4;
printf("Enter a Number ");
scanf("%d",&a);
if(a>=1000 && a<=9999)
{
printf("It is a 4 digit number");
n1=a%10;
n2=a/10%10;
n3=a/100%10;
n4=a/1000;
if(n1+n2==n3+n4)
printf("Sum is same") ;
else
printf("Sum is not same") ;
}
else
printf("It is not a 4 digit number");
}
11
C Programming | CCIT
WAP to read 3 angles and check if triangle can be formed or not. If triangle can be
formed then check if it is an isosceles or equilateral or right angled triangle.
#include<stdio.h> Enter a Angles 45 90 45
main() Triangle can be formed
{ .Iso. Triangle
int a,b,c;
printf("Enter 3 Angles ");
scanf("%d %d %d",&a,&b,&c);
if(a+b+c==180)
{
printf("Triangle can be formed");
if(a==b || b==c || c==a)
printf("\nIso. Triangle");
if(a==b && b==c)
printf("\nEq. Triangle");
if(a==90 || b==90 || c==90)
printf("\nRightAngled Triangl");
}
else
printf( "Triangle cannot be formed“ );
}
12
C Programming | CCIT
WAP to read marks for five subject & print total .Also print percentage if student is
passed(passing marks 40 per subject) & also check for Ist class.
#include<stdio.h> Enter marks for 5 subjects
main() 48 90 45 85 62
{ Student is passed
int a,b,c,d,e,t; Total is 330
float p; Percentage is 66.0
printf("Enter marks for 5 subjects");
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
t=a+b+c+d+e;
printf("\nTotal is %d",t) ;
if(a>=40 && b>=40 && c>=40 && d>=40 && e>=40)
{
printf("Student is Passed");
p=t*100/500.0;
printf("\nPercentage is %f",p);
if(p>=60)
printf("\nIst Class");
}
else
printf( "Student is failed“ ) ;
}
13
C Programming | CCIT
Ladder if statement
• If a if statement is used within an else
statement then such a control structure is
called as ladder if statement.
• A ladder if is used to check a condition if
some other condition is false
WAP to read a number and check if it is a positive no. or negative no or zero.
#include<stdio.h> .
Enter a Number 56
main() Number is positive
{
int a;
printf("Enter a Number");
scanf("%d",&a);
if(a>0)
printf("Number is Positive");
else
if(a<0)
printf("Number is Negative");
else
printf("Number is Zero");
}
14
C Programming | CCIT
WAP to read 3 different nos. a , b and c. and find greatest of them
#include<stdio.h>
main()
{
int a,b,c;
printf("Enter 3 different Numbers ");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
printf("%d is Greatest",a);
else
if(b>c)
printf("%d is Greatest",b);
else
printf("%d is Greatest",c);
}
Enter 3 different Numbers
31 7 78
Greatest number is 78
15
C Programming | CCIT
WAP to read color code i.e. a char value and print appropriate color according to
code [ r-red , g-green, b-blue or any other char-white].
#include<stdio.h>
main()
{
char a;
printf("Enter Color code ");
scanf("%c",&a);
if(a=='r'||a=='R')
printf("Red");
else
if(a=='g'||a=='G')
printf("Green");
else
if(a=='b'||a=='B')
printf("Blue");
else
printf("White");
}
Enter Color Code B
Blue
16
C Programming | CCIT
WAP to read gender code i.e. a char value and print appropriate gender
[m-male , f-female or any other char – invalid input]
#include<stdio.h>
main()
{
char a;
printf("Enter Gender code ");
scanf("%c",&a);
if(a=='m'||a=='M')
printf("Male");
else
if(a=='f'||a=='F')
printf("Female");
else
printf("Invalid Input");
}
Enter Gender Code F
Female
17
C Programming | CCIT
WAP to read a char. and check if it is an alphabet(a-z or A-Z) or digit(0-9) or
other char.
#include<stdio.h>
main()
{
char ch;
printf("Enter a Char :");
scanf("%c",&ch);
if((ch>='A' && ch<='Z')||(ch>='a' && ch<='z'))
printf("Alphabets");
else
if(ch>='0' && ch<='9')
printf("Digits");
else
printf("Other Char");
}
Enter a Char : P
Alphabet
18
C Programming | CCIT
19