Introduction
In this tutorial, you will learn how to write a C program to print the maximum among three numbers by using if-else statement. We have also explained some examples, algorithm, and programs for your better understanding.
Problem Statement
Write a C program that reads three numbers from the user. After taking the inputs it should determine the maximum among them using the “if-else” statement. The program should display the maximum number as output.
Example
Enter the first number: 8
Enter the second number: 12
Enter the third number: 15
The maximum number is 15
In the above example 15 is printed as an output means it is a greatest among other (8, 12). So Here the Output will be 15 and it is correct.
Algorithm
- Start the program.
- Read the first number and store it in a variable, let’s say
num1
. - Read the second number and store it in another variable, let’s say
num2
. - Read the third number and store it in a third variable, let’s say
num3
. - Compare
num1
,num2
, andnum3
using the “if-else” statement: a. Ifnum1
is greater than bothnum2
andnum3
, printnum1
as the maximum number. b. Ifnum2
is greater than bothnum1
andnum3
, printnum2
as the maximum number. c. Otherwise, printnum3
as the maximum number. - End the program.
Program To Print Maximum Among Three Numbers
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Enter the third number: ");
scanf("%d", &num3);
if (num1 > num2 && num1 > num3) {
printf("The maximum number is %d.\n", num1);
} else if (num2 > num1 && num2 > num3) {
printf("The maximum number is %d.\n", num2);
} else {
printf("The maximum number is %d.\n", num3);
}
return 0;
}
Output
Enter the first number: 65
Enter the second number: 23
Enter the third number: 45
The maximum number is 65
Program Explanation
- We start by including the necessary header file
stdio.h
for input/output operations. - The
main()
function is the entry point of the program. - We declare three variables
num1
,num2
, andnum3
to store the input numbers. - We prompt the user to enter the first number using
printf()
. - The
scanf()
function is used to read the first number and store it in the variablenum1
. - We repeat steps 4 and 5 for the second and third numbers.
- Using the “if-else” statement, we compare
num1
,num2
, andnum3
. - If
num1
is greater than bothnum2
andnum3
, we printnum1
as the maximum number. - If
num2
is greater than bothnum1
andnum3
, we printnum2
as the maximum number. - Otherwise, we print
num3
as the maximum number. - Finally, the program ends and returns 0 to the operating system.
Conclusion
In conclusion, the C program we developed successfully solves the problem of finding the maximum among three numbers using the if-else statement. It allows the user to input three numbers, compares them, and displays the maximum number as the output. The program demonstrates the use of conditional statements and input/output operations in C programming. By understanding this program, you have gained knowledge of how to make decisions based on conditions and perform basic input/output operations in C.