For video
tutorial you can see below video:
C if...else Ladder:
The if...else statement
executes two different codes depending upon whether the test expression is true
or false. Sometimes, a choice has to be made from more than 2 possibilities.
The if...else
ladder allows you to check between multiple test expressions and execute
different statements.
Syntax of if...else Ladder:
if (test expression1) {
// statement(s)
}
else if(test expression2) {
// statement(s)
}
else if (test expression3) {
// statement(s)
}
.
.
else {
// statement(s)
}
Example 1: C if...else Ladder:
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter Number: \n");
scanf("%d",&a);
if(a>0)
{
printf("Number Is Positive ");
}
else if(a<0)
{
print("Number is Negative");
}
else
{
printf("Number is Zero");
}
getch();
}
Output:
If user enter 5 then output will " Number is Positive".
If user enter -5 then output will " Number is Negative".
If user enter 0 then output will " Number is Zero".
Example 2: C if...else Ladder:
#include<stdio.h>
#include<conio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter Age: \n");
scanf("%d",&a);
if(a>=18 && a<60)
{
printf(" Person is Young ");
}
else if(a<60)
{
print("Person is Old");
}
else
{
printf("Person is Child");
}
getch();
}
If user enter 20 then output will " Person is Young ".
If user enter 67 then output will " Person is Old".
If user enter 11 then output will " Person is Child".
Example 3: C if...else Ladder:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the value of A: \n");
scanf("%d",&a);
printf("Enter the value of B: \n");
scanf("%d",&b);
if(a>b)
{
printf(" A is greater than B ");
}
else if(a<b)
{
print("B is greater than A");
}
else
{
printf("Both Number Are Equal");
}
getch();
}
If user enter a=40 and b=33 then output will " A is greater than B ".
If user enter b=67 and a=50 then output will " B is greater than A ".
If user enter a=11 and b=11 then output will " Both Are Equal".
Example 4: C if...else Ladder:
#include<stdio.h>
#include<conio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter the Marks: \n");
scanf("%d",&a);
if(a>80)
{
printf(" A Grade");
}
else if(a>65)
{
print("B Grade");
}
else if(a>50)
{
printf("C Grade");
}
else if(a>35)
{
printf("Pass Class");
}
else (a<35)
{
printf("Fail");
}
getch();
}
If user enter 85 then output will " A Grade ".
If user enter 67 then output will " B Grade".
If user enter 55 then output will " C Grade".
If user enter 47 then output will "Pass Class".
If user enter 23 then output will "Fail".
Example 5: C if...else Ladder:
#include<stdio.h>
#include<conio.h>
#include<conio.h>
void main()
{
double a;
clrscr();
printf("Enter Salary: \n");
scanf("%lf",&a);
if(a>70000)
{
printf("Employee");
}
else if(a>55000)
{
print("Supervisor");
}
else if(a>40000)
{
printf("Manager");
}
else if(a>20000)
{
printf("Worker");
}
else (a<35)
{
printf("Fail");
}
getch();
}
If user enter 77000 then output will "Employee".
If user enter 60000 then output will "Supervisor".
If user enter 43000 then output will "Manager".
If user enter 22000 then output will "Worker".
If user enter 11000 then output will "Fail".
Example 6: C if...else Ladder:
#include<stdio.h>
#include<conio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter the value of a: \n");
scanf("%d",&a);
if(a%5==0)
{
printf("Number is divisible by 5");
}
else if(a%7==0)
{
print("Number is divisible by 7");
}
else
{
printf("Number is not divisible by 5 and 7");
}
getch();
}
Output:
If user enter 25 then output will " Number is divisible by 5".
If user enter 49 then output will " Number is divisible by 7".
If user enter 16 then output will "Number is not divisible by 5 and 7".
0 Comments
If any one want learn some specific program in Computer Languages, so They all can comment there program questions.