This program is similar to the previous one, with a single modification, here we will use if else statement to find the largest number.
Logic
The logic to find the largest number among the three numbers, we have to compare each number with the other two numbers.
Let three variables be: A = 400, B = 200 and C = 300
- if A > B and A > C, then print A.
- else if B > A and B > C, then print B.
- else print C.
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 && num1 >= num3) { printf("\n%d is largest number", num1); } 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 : 10
Enter 2nd number : 200
Enter 3rd number : 11
200 is largest number