C Program To Find the Largest Number Among Three Numbers

In this post, you will learn to find the largest number among three numbers.

C Program To Find the Largest Number Among Three Numbers

This program compares the three input integer numbers provided by the user. And then the largest of the three is displayed on the screen.

So, Without further ado, let’s begin the tutorial.

C Program To Find the Largest Number Among Three Numbers

// C Program To Find the Greatest Among Three Numbers
#include <stdio.h>
int main(){
    int a, b, c;
    
    // Asking for Input
    printf("Enter Three Numbers: ");
    scanf("%d %d %d", &a, &b, &c);
    
    // logic
    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 Three Numbers: 3 5 7
7 is the largest number.

How Does This Program Work ?

    int a, b, c;

In this program, we have declared three int variable names as a, b and c respectively.

    // Asking for Input
    printf("Enter Three Numbers: ");
    scanf("%d %d %d", &a, &b, &c);

Then, the user is asked to enter the three numbers. The value of these three numbers will be stored in a, b and c variables.

    // logic
    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);

Finally, a simple logic is used in this program to get the desirable output. If a is greater than both b and c , then clearly a is the greatest.

If b is greater than both a and c, then b will be the greatest of the three.

And if neither of the above conditions are true, then automatically c will be the largest number.

Some used terms are as follow: 

#include <stdio.h> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program. 

The stdio.h is a file which contains input and output functions like scanf() and printf() to take input and display output respectively.  

Int main() – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function.  

scanf() – scanf() function is used to take input from the user.  

printf() – printf() function is used to display and print the string under the quotation to the screen. 

If. . . else – An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. If Statement executes when the Boolean expression is True. 

ValueImpression Mobile Ads === codingbroz.com_300x250 (#88863) ===
=== codingbroz.com_300x250 (#88863) ===

&& – It is called Logical AND Operator. If both of the statements are non-zero, then the condition becomes true. 

// – Used for Commenting in C. 

Conclusion

I hope after going through this tutorial, you understand how to find the largest number among three numbers.

If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Leave a Comment

Your email address will not be published. Required fields are marked *