If else-if ladder Statement
The if else-if ladder statement is an extension of the if-else statement. It is used in the scenario where there are multiple cases to be performed for different conditions. In if else-if ladder statement, if a condition is true then the statements defined in the if block will be executed, otherwise if some other condition is true then the statements defined in the else-if block will be executed, at the last if none of the condition is true then the statements defined in the else block will be executed. There are multiple else-if blocks possible.
#include <stdio.h>
int main()
{
int number;
printf("Enter a number : ");
scanf("%d", &number);
if (number == 10){
printf("Number is equals to 10.");
}
else if (number == 50){
printf("Number is equals to 50.");
}
else if (number == 100){
printf("Number is equals to 100.");
}
else{
printf("Number is not equal to 10, 50 or 100.");
}
return 0;
}
Output :
Enter a number : 50
Number is equals to 50.
Enter a number : 75
Number is not equal to 10, 50 or 100.
Program to calculate the grade of the student according to the specified marks
#include <stdio.h>
int main()
{
int marks;
printf("Enter your marks : ");
scanf("%d", &marks);
if (marks > 85 && marks <= 100){
printf("Congrats!! You scored grade A .....");
}
else if (marks > 60 && marks <= 85){
printf("You scored grade B+ .....");
}
else if (marks > 40 && marks <= 60){
printf("You scored grade B .....");
}
else if (marks > 30 && marks <= 40){
printf("You scored grade C .....");
}
else {
printf("Sorry, You are fail .....");
}
return 0;
}
Output :
Enter your marks : 10
Sorry, You are fail .....
Enter your marks : 95
Congrats!! You scored grade A .....
Enter your marks : 40
You scored grade C .....
Nested-if in C
A nested if in C is an if statement that is the target of another if statement. Nested if statements means an if statement inside another if statement.
#include <stdio.h>
int main()
{
int x, y;
printf("Enter two numbers : ");
scanf("%d %d", &x, &y);
if (x != y){
if (x>y){
printf("%d is greater.", x);
}
else{
printf("%d is greater.", y);
}
}
else{
printf("Both numbers are equal.");
}
return 0;
}
Output :
Enter two numbers : 8 2
8 is greater.
Enter two numbers : 2 8
8 is greater.
Enter two numbers : 8 8
Both numbers are equal.
Comments
Post a Comment