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:

  1. Include Header Files: Include stdio.h for input/output functions.
  2. Declare Variables: Use three variables to store the numbers entered by the user.
  3. Take Input: Ask the user to enter three numbers.
  4. Compare the Numbers: Use conditional statements to compare the three numbers and determine the largest.
  5. Display the Result: Print the largest number.

Program:

#include <stdio.h> int main() { int num1, num2, num3; // Declare variables to store three numbers // Ask the user to input three numbers printf("Enter three numbers: "); scanf("%d %d %d", &num1, &num2, &num3); // Compare the numbers and find the maximum if (num1 >= num2 && num1 >= num3) { // If num1 is greater than or equal to both num2 and num3, it is the largest printf("The largest number is: %d\n", num1); } else if (num2 >= num1 && num2 >= num3) { // If num2 is greater than or equal to both num1 and num3, it is the largest printf("The largest number is: %d\n", num2); } else { // Otherwise, num3 is the largest printf("The largest number is: %d\n", num3); } return 0; }

Explanation:

  1. Variables:

    • int num1, num2, num3: These three variables store the three numbers input by the user.
  2. 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 in num1, num2, and num3.
  3. Comparing the Numbers:

    • The condition if (num1 >= num2 && num1 >= num3) checks if num1 is greater than or equal to both num2 and num3. If true, num1 is the largest number.
    • The else if (num2 >= num1 && num2 >= num3) checks if num2 is greater than or equal to both num1 and num3. 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.
  4. Output:

    • Based on the comparison, the largest number is printed using the printf function.

Sample Output:

Example 1:

Enter three numbers: 5 9 2 The largest number is: 9

Example 2:

Enter three numbers: 12 12 10 The largest number is: 12

Example 3:

Enter three numbers: 8 3 15 The largest number is: 15

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.