C Program To Print Maximum Among Three Numbers Using If-else.

C Program To Print Maximum Among Three Numbers Using If-else

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

  1. Start the program.
  2. Read the first number and store it in a variable, let’s say num1.
  3. Read the second number and store it in another variable, let’s say num2.
  4. Read the third number and store it in a third variable, let’s say num3.
  5. Compare num1, num2, and num3 using the “if-else” statement: a. If num1 is greater than both num2 and num3, print num1 as the maximum number. b. If num2 is greater than both num1 and num3, print num2 as the maximum number. c. Otherwise, print num3 as the maximum number.
  6. 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, and num3 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 variable num1.
  • We repeat steps 4 and 5 for the second and third numbers.
  • Using the “if-else” statement, we compare num1, num2, and num3.
  • If num1 is greater than both num2 and num3, we print num1 as the maximum number.
  • If num2 is greater than both num1 and num3, we print num2 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.