You are currently viewing C Program to Print Maximum Among two Numbers using If Else

C Program to Print Maximum Among two Numbers using If Else

In this tutorial, you will learn to write C Program print maximum among two numbers using if-else Statement. Here, we have written and explained the algorithms with suitable examples for a better understanding of this program.

Problem Statement

We have to write a program in C Programming language that reads two numbers from the user. After taking the input it should determine the maximum among them. For this program we will have to use “if-else” statement. The program should display the maximum number as output.

Example

Enter the first number: 8

Enter the second number: 12

The maximum number is 12

In the above example as a user has given two numbers 8 and 12. Here 12 is greater than 8 means the maximum is 12. So the output should be 8.

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. Compare num1 and num2 using the “if-else” statement: a. If num1 is greater than num2, print num1 as the maximum number. b. Otherwise, print num2 as the maximum number.
  5. End the program.

Program to print maximum among two numbers using if-else

#include <stdio.h>
int main() {
    int num1, num2;
    printf("Enter the first number: ");
    scanf("%d", &num1);
    printf("Enter the second number: ");
    scanf("%d", &num2);
    if (num1 > num2) {
        printf("The maximum number is %d.\n", num1);
    } else {
        printf("The maximum number is %d.\n", num2);
    }
    return 0;
}

Output

Enter the first number: 65
Enter the second number: 78
The maximum number is 78

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 two variables num1 and num2 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 number.
  • Using the “if-else” statement, we compare num1 and num2.
  • If num1 is greater than num2, we print num1 as the maximum number.
  • If num2 is greater than or equal to num1, we print num2 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 two numbers using the if-else statement. It allows the user to input two 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.