C Program to Find the Maximum of Three Numbers
Program to find the maximum of three numbers.
Program to find the maximum of three numbers.
Given three numbers a, b, and c, write a program to find the maximum of these numbers.
Input:
a = 10, b = 20, c = 30
Output:
30
Explanation:
Out of 10, 20, and 30, 30 is the largest.
Input:
a = 10, b = 20, c = 20
Output:
20
Explanation:
Out of 10, 20, and 20, 20 is the largest.
Input:
a = 10, b = 10, c = 10
Output:
10
Explanation:
10 is compared with itself twice. Therefore, 10 is the largest.
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. 🙂
To verify the program you have written, go ahead and test your program using the following test cases:
Input | Output | |
---|---|---|
Test Case 1 | 10 20 30 | 30 is the largest. |
Test Case 2 | 4000 4001 2 | 4001 is the largest. |
Test Case 3 | 0 0 0 | 0 is the largest. |
Test Case 4 | 10 10 5 | 10 is the largest. |
Following are the multiple approaches to the same problem:
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.
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.
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