Find the Largest Number Amongst Three Numbers in C

Exercise:

Write a C Program to find the largest number among three numbers.

1. Find using if Statement

Click Here to View the Solution!
#include <stdio.h>   
int main() {
    double num1, num2, num3;
    printf("Enter three different numbers: ");
    scanf("%lf %lf %lf", &num1, &num2, &num3);

    // if n1 is greater than both num22 and num3
    if (num1 >= num2 && num1 >= num3) {
        printf("%.2f is the largest number.", num1);
    }
    // if num2 is greater than both num1 and num3   
    if (num2 >= num1 && num2 >= num3) {
        printf("%.2f is the largest number.", num2);
    }
    // if num3 is greater than both num1 and num2
    if (num3 >= num1 && num3 >= num2) {
        printf("%.2f is the largest number.", num3);
    }
    return 0;
}
Click Here to View the Output!
Enter three different numbers: 6 7 3
7.00 is the largest number. 
Click Here to View the Explanation!
  • This program is used to find the largest number among three numbers using the if statement.
  • In the main method, the program requests the user to enter three numbers and store them into the double type variables num1, num2 and num3 using the scanf() function.
  • The program uses three if statements each of which checks whether num1, num2 or num3 is the largest number in comparison to one another.
  • The largest number when found is printed and displayed on the screen.
  • The return 0 statement is used to end the program.

2. Find using if…else ladder

Click Here to View the Solution!
#include <stdio.h>
int main() {
    double num1, num2, num3;
    printf("Enter three numbers: ");
    scanf("%lf %lf %lf", &num1, &num2, &num3);

    // if num1 is greater than both num2 and num3
    if (num1 >= num2 && num1 >= num3) {
        printf("%.2lf is the largest number.", num1);
    }

    // if num2 is greater than both num1 and num3
    else if (num2 >= num1 && num2 >= num3) {
        printf("%.2lf is the largest number.", num2);
    }

    // if both conditions are false num3 is the largest
    else {
        printf("%.2lf is the largest number.", num3);
    }
    return 0;
}
Click Here to View the Output!
Enter three numbers: 5 8 2                           
8.00 is the largest number.
Click Here to View the Explanation!
  • This program is used to find the largest number among three numbers using the if…else statement.
  • In the main method, the program requests the user to enter three numbers and store them into the double type variables num1, num2 and num3 using the scanf() function.
  • Initially, the if statement compares num1 with both num2 and num3 and if num1 is larger than both then the message num1 is the largest number is printed.
  • If the if condition is false, the if…else statement is checked which compares num2 with both num1 and num3 and if num2 is larger than both then the message num2 is the largest number is printed.
  • If both the first two conditions are false, the else statement is executed which prints num3 is the largest number.
  • The return 0 statement is used to exit the program execution.

3. Find using Nested if…else

Click Here to View the Solution!
#include <stdio.h>        
int main() {
    double num1, num2, num3;
    printf("Enter three numbers: ");
    scanf("%lf %lf %lf", &num1, &num2, &num3);
    if (num1 >= num2) {
        if (num1 >= num3) {
            printf("%.2lf is the largest number.", num1);
        }
        else { 
            printf("%.2lf is the largest number.", num3);
        }
    } 
    else {
        if (num2 >= num3) {
            printf("%.2lf is the largest number.", num2);
        }
        else {
            printf("%.2lf is the largest number.", num3);
        }
    }
    return 0;
}
Click Here to View the Output!
Enter three numbers: 3 9 6 
9.00 is the largest number.
Click Here to View the Explanation!
  • This program is used to find the largest number among three numbers using the nested if…else statement.
  • In the main method, the program requests the user to enter three numbers and store them into the double type variables num1, num2 and num3 using the scanf() function.
  • The outer if statement checks whether num1 is greater than num2 and contains another if…else statement which compares num1 with num3 and prints that num1 is the largest number if both the outer and inner if statements are true. Else, the message num3 is the largest number is printed by the inner else statement.
  • The outer else statement contains another if…else statement which compares num2 with num3 and prints that num2 is the largest number if the outer if statement is false and inner if statement inside the outer else statement is true. Else, the message num3 is the largest number is printed by the inner else statement.
  • The return 0 statement is used to exit the program execution.

%d bloggers like this: