In this program, we are going to find the largest number among three numbers, similar to the previous one, but it is nested if-else version.
Logic
Let three variables be: A = 400, B = 200 and C = 300
The logic goes like this:
- if A >= B then check for if A >= C, then print A else print C.
- else part: if B >= C then print B else print C.
See the below-shown flow diagram to better understand.
Program
#include <stdio.h> int main() { int num1, num2, num3; printf("Enter 1st number : "); scanf("%d", &num1); printf("\nEnter 2nd number : "); scanf("%d", &num2); printf("\nEnter 3rd number : "); scanf("%d", &num3); if(num1 >= num2) { if(num1 >= num3) { printf("\n%d is largest number", num1); } else { printf("\n%d is largest number", num3); } } else { if(num2 >= num3) { printf("\n%d is largest number", num2); } else { printf("\n%d is largest number", num3); } } return 0; }
Output
Enter 1st number : 100
Enter 2nd number : 10
Enter 3rd number : 99
100 is largest number