C Program to Find Largest of Three Numbers

Howdy readers, today you will learn how to find the largest of three numbers using C Programming language.

C Program to Find Largest of Three Numbers

This program asks the user to enter three numbers, then it finds the largest of them using if-else statement and comparison operators.

So, without any delay, let’s begin this tutorial.

C Program to Find Largest of Three Numbers

C Program

// C Program to Find Largest of Three Numbers
#include <stdio.h>
int main(){
    int a, b, c;
    
    // Asking for input
    printf("Enter the first number: ");
    scanf("%d", &a);
    printf("Enter the second number: ");
    scanf("%d", &b);
    printf("Enter the three number: ");
    scanf("%d", &c);
    
    // Finding largest number
    if (a >= b && a >= c){
        printf("%d is the largest number.", a);
    }
    else if (b >= a && b >= c){
        printf("%d is the largest number.", b);
    }
    else{
        printf("%d is the largest number.", c);
    }
    
    return 0;
}

Output

Enter the first number: 7
Enter the second number: 12
Enter the three number: 5
12 is the largest number.
C Program to Find Largest of Three Numbers

Explanation

    int a, b, c;

In this program, we have declared three int data type variables named a, b and c.

    // Asking for input
    printf("Enter the first number: ");
    scanf("%d", &a);
    printf("Enter the second number: ");
    scanf("%d", &b);
    printf("Enter the three number: ");
    scanf("%d", &c);

Then, the user is asked to enter three numbers. These numbers get stored in the a, b and c named variables respectively.

    if (a >= b && a >= c){
        printf("%d is the largest number.", a);
    }

Now, we check whether a is greater than b and c or not. If the condition is True, then a is the largest number and we print it on the screen using printf() function.

    else if (b >= a && b >= c){
        printf("%d is the largest number.", b);
    }

Similarly, we check whether b is greater than a and c or not. If this statement is True, then b is the largest of the three.

    else{
        printf("%d is the largest number.", c);
    }

If neither of the above two statements are True, then it clearly means c is the largest number and we print it’s value on the screen using printf() function.

Conclusion

I hope after reading this post, you understand how to write a program to find the largest of three numbers using C Programming language.

If you have any problem regarding the program, then ask your query in the comment section. We will be delighted to solve your query.

Also Read:

Leave a Comment