In this program we’ll show you nesting of if else statements. Here we are illustrating the concept by taking score/marks of 5 subject from the user. Then we calculate the percentage of it and display grade to the user.
When the condition in if statement is false then only else block gets executed. In our program, we check, if percentage is greater than or equal to 60. If it is false, then only control shifts to else block. So inside else(nested if else) we need not once again check if the percentage is less than 60.
Note: We are not using else if and logical operator in this C program purposefully. We want to show nesting of if else in this program.
#include < stdio.h > int main() { float s1, s2, s3, s4, s5, per; printf("Enter marks of 5 subject\n"); scanf("%f %f %f %f %f", &s1, &s2, &s3, &s4, &s5); per = (s1 + s2 + s3 + s4 + s5) / 5.0; if(per >= 60) { printf("Grade A\n"); } else { if(per >= 50) { printf("Grade B\n"); } else { if(per >= 40) { printf("Grade C\n"); } else { printf("Failed!\n"); } } } return 0; }
Output 1:
Enter marks of 5 subject
59
60
62
61
63
Grade A
Output 2:
Enter marks of 5 subject
59
55
56
53
58
Grade B
Output 3:
Enter marks of 5 subject
49
45
46
49
48
Grade C
Output 4:
Enter marks of 5 subject
39
45
25
36
35
Failed!
Student Grade Calculation using Nested if else: C Program
Note: In next video we’ll show you how to perform the same operation as shown in above C program, using else-if and logical operators.
Also check the program C Program To Check Leap Year for a perfect nested if else example.
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert