C Program to Find the Maximum of Three Numbers

Program to find the maximum of three numbers.


Problem

Given three numbers a, b, and c, write a program to find the maximum of these numbers.

Example 1:

Input:
a = 10, b = 20, c = 30

Output:
30

Explanation:
Out of 10, 20, and 30, 30 is the largest.

Example 2:

Input:
a = 10, b = 20, c = 20

Output:
20

Explanation:
Out of 10, 20, and 20, 20 is the largest.

Example 3:

Input:
a = 10, b = 10, c = 10

Output:
10

Explanation:
10 is compared with itself twice. Therefore, 10 is the largest.

Expectations:

You need to write the complete program using the following template as the starter:

#include <stdio.h>

int main() {
    int a, b, c;
    
    // The prompt.
    printf("Enter the three numbers: ");
    scanf("%d %d %d", &a, &b, &c);
    
    // Write your code here.
    
    return 0;
}

Copy and paste the above code in your favorite IDE and start writing your code after the comment // Write your code here.

Pro tip:  
First, try writing the code on your own. Try every possible thing you can to come up with the solution. If you still can’t figure it out, then check the solution and see what went wrong. This is how you learn. 🙂

Sample Test Cases:

To verify the program you have written, go ahead and test your program using the following test cases:

InputOutput
Test Case 110 20 3030 is the largest.
Test Case 24000 4001 24001 is the largest.
Test Case 30 0 00 is the largest.
Test Case 410 10 510 is the largest.

Solution

Following are the multiple approaches to the same problem:

Using Nested if-else

We can use the nested if-else structure to compare the values of a, b, and c to come up with the final result. Here is how we can go about it:

#include <stdio.h>

int main() {
    int a, b, c;
    
    // The prompt
    printf("Enter the three numbers: ");
    scanf("%d %d %d", &a, &b, &c);
    
    // Write your code here.
    if (a >= b) { 
        if (a >= c) {
            printf("%d is the largest.\n", a);
        } else {
            printf("%d is the largest.\n", c);
        }
    } else if (b >= c) { // a < b and b >= c means b is the largest 
        printf("%d is the largest.\n", b);
    } else { // a < b and b < c means c is the largest
        printf("%d is the largest.\n", c);
    }
    return 0;
}

Output:

Enter the three numbers: 30 20 10
30 is the largest.

Using the Logical Operators

To simplify the code, we can combine multiple conditions in the same if statement using logical operators. In this way, we can avoid writing nested if-else. The code is as follows:

#include <stdio.h>

int main() {
    int a, b, c;
    
    // The prompt.
    printf("Enter the three numbers: ");
    scanf("%d %d %d", &a, &b, &c);
    
    // Write your code here.
    int max = a;
    if (b >= a && b >= c) {
        max = b;
    } else if (c >= a && c >= b) {
        max = c;
    }
    
    printf("%d is the largest number.\n", max);
    
    return 0;
}

Output:

Enter the three numbers: 10 10 5
10 is the largest number.

We’re tracking the maximum number using the variable max. This lets us write a single printf() function at the end of the code, making it more compact and readable. We’ve also eliminated the need for a nested-if statement by using the logical AND operator (&&) to combine our conditions, which also contributes to the code’s compactness.

Further Improvisation

The above program can be simplified even further. We have already set the max to ‘a’. We just need to compare it with b and c separately and set the max variable accordingly. Here is the program:

#include <stdio.h>

int main() {
    int a, b, c;
    
    // The prompt.
    printf("Enter the three numbers: ");
    scanf("%d %d %d", &a, &b, &c);
    
    // Write your code here.
    int max = a;
    if (b > max) {
        max = b;
    }
    // Don't use "if else" as we want to check the following condition
    // even when b > max is satisfied.
    if (c > max) {
        max = c;
    }
    
    printf("%d is the largest number.\n", max);
    
    return 0;
}

Output:

Enter the three numbers: 10 200 2
200 is the largest number.


Leave a comment

Leave a comment

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

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.