In this program we are going to see how to find the largest number among three numbers using if statement.

Logic

Here we have to compare each number with another two numbers, if it is greater than the both then simply print it.

Let’s say A = 11, B = 22 and C = 15

Then steps would be

  1. if A > B and A > C that means A is the largest number.
  2. if B > A and B > C that means B is the largest number.
  3. Similarly if C > A and C > B that means C is the largest number.

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);
    }

    if(num2 >= num1 && num2 >= num3) {

        printf("\n%d is largest number", num2);
    }

    if(num3 >= num1 && num3 >= num2) {

        printf("\n%d is largest number", num3);
    }

    return 0;
}

Output

Enter 1st number : 10
Enter 2nd number : 5
Enter 3rd number : 11
11 is largest number