C Find Maximum of Three Numbers Program
In C, a Find Maximum of Three Numbers program determines the largest number among three numbers entered by the user. This can be achieved by using conditional statements (if-else
or nested if
) to compare the numbers and identify the maximum.
Steps for the Program:
- Include Header Files: Include
stdio.h
for input/output functions. - Declare Variables: Use three variables to store the numbers entered by the user.
- Take Input: Ask the user to enter three numbers.
- Compare the Numbers: Use conditional statements to compare the three numbers and determine the largest.
- Display the Result: Print the largest number.
Program:
Explanation:
Variables:
int num1, num2, num3
: These three variables store the three numbers input by the user.
Input:
printf("Enter three numbers: ");
: This statement prompts the user to enter three numbers.scanf("%d %d %d", &num1, &num2, &num3);
: This reads the three numbers entered by the user and stores them innum1
,num2
, andnum3
.
Comparing the Numbers:
- The condition
if (num1 >= num2 && num1 >= num3)
checks ifnum1
is greater than or equal to bothnum2
andnum3
. If true,num1
is the largest number. - The
else if (num2 >= num1 && num2 >= num3)
checks ifnum2
is greater than or equal to bothnum1
andnum3
. If true,num2
is the largest number. - If neither of the above conditions is true, the program concludes that
num3
is the largest number and prints it.
- The condition
Output:
- Based on the comparison, the largest number is printed using the
printf
function.
- Based on the comparison, the largest number is printed using the
Sample Output:
Example 1:
Example 2:
Example 3:
Key Points:
%d
is the format specifier used to read and print integers.- Conditional Logic: The program uses
if-else if-else
statements to compare the three numbers and determine the largest one. - Handling Equal Numbers: The program correctly handles cases where two or more numbers are equal, and still outputs the maximum number.
This program is a simple example of using conditional logic in C to find the largest of three numbers based on user input.