Syntax: if(condition)
{
Statement 1
....
....
Statement n
}
Flowchart
EX: Program to check whether the entered number is positive negative or zero
#include<stdio.h>
#include<conio.h>
Void main()
{
int n;
clrscr();
printf(“\n Enterd any number=â€);
scanf(“%d†,&n);
if(n>o)
printf(“\n %d is positiveâ€,n);
if(n<0)
printf(“\n %d is negativeâ€,n);
if(n==0)
printf(“\n %d is zeroâ€,n)
getch();
}
b. if Else
Syntax: if(condition)
{
Statement 1;
}
else
{
Statement 2;
}
Flowchart
Ex: program to check whether the entered number is even or odd.
#include<stdio.h>
#include<conio.h>
Void main()
{
int n;
clrscr();
printf(“Enter any number \nâ€);
scanf(“%dâ€,&n);
if(n%2==0)
{
printf(“\n %d is evenâ€,n);
}
else
{
Printf(“\n %d is oddâ€,n);
}
getch();
}
c. Nested else if
Syntax: if(condition 1)
{
if(condition 2)
Statement 1;
else
Statement 2;
}
else
{
if(condition 3)
Statement 3;
else
statement 4;
}
Flowchart
EX: Program to check biggest number among three number.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c, big;
Printf(“Enter three numbers :â€);
Scanf(“%d%d%dâ€, &a,&b,&c);
if(a>b)
{
if(a>c)
big=a;
else
big=c;
}
else
{
if(b>c)
big=b;
else
big=c;
}
Printf(“Biggest number is %d \nâ€,big);
getch();
}
d. Ladder else if
Syntax: if (Test Condition -1)
Statement -1;
else if ( Test Condition -2)
Statement -2;
else if ( Test Condition -3)
Statement -3;
:
:
else if ( Test Condition –n)
Statement –n;
else
default statement;
Rest of the Program Statements-X;
Flowchart
EX: Program to find out the grade of a student when the marks of 4 subject are given.
#include<stdio.h>
#include<conio.h>
main()
{
float m1,m2,m3,m4,total,per;
char grade;
clrscr();
printf("Enter marks of subject:");
scanf("%f%f%f%f",&m1,&m2,&m3,&m4);
total = m1+m2+m3+m4;
per = total/4.0;
if(per>85)
grade='A';
else if(per>=75)
grade='B';
else if(per>=55)
grade='C';
else if(per>=40)
grade='D';
else if(per>=40)
grade='E';
printf("percentage of %f, Grade is %c, \n",per,grade);
getch();
}