In this article if else program in c we give the simple if statement programs , if else statements program and nested if else program with output.
// 1. Write a C Program to find given year is Leap year or not
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf(“\n\n Enter the year: “);
scanf(“%d”,&year);
if(year%4==0 || year%400==0)
{
printf(“\n\n %d is Leap year…”,year);
}
else
if(year%4!=0 || year%100==0)
{
printf(“\n\n %d is Not Leap year…”,year);
}
getch();
}
Output:
Enter the year: 2024
2024 is Leap year…
// 2. Simple if statements – to find person is eligible for voting.
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf(“\n\n Enter the age: “);
scanf(“%d”,&age);
if(age>=18)
{
printf(“\n\n Parson is eligible for voting.”);
}
printf(“\n\n Have a nice Day!…”);
getch();
}
Output:
Enter the age: 20
Parson is eligible for voting.
Have a nice Day!…
// 3. Simple if statements – to find person is retired.
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf(“\n\n Enter the age: “);
scanf(“%d”,&age);
if(age>=58)
{
printf(“\n\n Parson is retired.”);
}
printf(“\n\n Have a nice Day!…”);
getch();
}
Output:
Enter the age: 60
Parson is retired.
Have a nice Day!…
// 4. if…else statements – to find max number among 2 diff. numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf(“\n\n Enter the 2 numbers: “);
scanf(“%d%d”,&x,&y);
if(x>y)
{
printf(“\n\n First number is largest number: %d”,x);
}
else
{
printf(“\n\n Second number is largest number: %d”,y);
}
getch();
}
Output:
Enter the 2 numbers: 17
25
Second number is largest number: 25
// 5. if…else statements – to find small number among 2 diff. numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf(“\n\n Enter the 2 numbers: “);
scanf(“%d%d”,&x,&y);
if(x<y)
{
printf(“\n\n First number is small number: %d”,x);
}
else
{
printf(“\n\n Second number is small number: %d”,y);
}
getch();
}
Output:
Enter the 2 numbers: 23
46
First number is small number: 23
/* 6. Write a C Program to find Max number
Among 3 different numbers…*/
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
clrscr();
printf(“\n\n Enter the diff. 3 numbers: “);
scanf(“%d%d%d”,&x,&y,&z);
if(x>y && x>z) // x=20, y=100, z=500
{
printf(“\n\n First number is largest: %d”,x);
}
else
if(y>z)
{
printf(“\n\n Second number is largest: %d”,y);
}
else
{
printf(“\n\n Third number is largest: %d”,z);
}
getch();
}
Output:-
Enter the diff. 3 numbers: 68
35
269
Third number is largest: 269
// 7. if…else statements – To find given number is odd or even
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“\n\n Enter the number: “);
scanf(“%d”,&n);
if(n%2==0)
{
printf(“\n\n %d is Even Number…”,n);
}
else
{
printf(“\n\n %d is Odd Number…”,n);
}
printf(“\n\n %d is also Natuaral number…”);
getch();
}
Output:
Enter the number: 13
13 is Odd Number.
// 8. Nested if..else statements…To find given number is +ve, -ve or neutral number
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“\n\n Enter the number: “);
scanf(“%d”,&n);
if(n>0)
{
printf(“\n\n %d is +ve number…”);
}
else
if(n<0)
{
printf(“\n\n %d is -ve number….”);
}
else
{
printf(“\n\n %d is Neutral…”);
}
getch();
}
Output: – Enter the number: 125
125 is +ve number…
/* 9. Write a C program to accept 3 subject marks and show total marks, percentage and grade. */
#include<stdio.h>
#include<conio.h>
void main()
{
int c=0,cpp=0,java=0,tot=0;
float per=0;
clrscr();
printf(“\n\n Enter the 3 subject marks: “);
scanf(“%d%d%d”,&c,&cpp,&java);
tot=c+cpp+java;
printf(“\n\n Total marks: %d”,tot);
per=tot/3;
printf(“\n\n Percentage: %.2f”,per);
if(c>=40 && cpp>=40 && java>=40)
{
printf(“\n\n Result: Pass”);
}
else
{
printf(“\n\n Result: Fail”);
}
if(per>=40 && per<50)
{
printf(“\n\n Grade: C”);
}
else
if(per>=50 && per<60)
{
printf(“\n\n Grade: B”);
}
else
if(per>=60 && per<70)
{
printf(“\n\n Grade: A”);
}
else
if(per>=70 && per<80)
{
printf(“\n\n Grade: A+”);
}
else
if(per>=80)
{
printf(“\n\n Grade: O”);
}
getch();
}
Output:-
Enter the 3 subject marks: 80
90
70
Total marks: 240
Percentage: 80.00
Result: Pass
Grade: O
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining