C Program to Find Maximum of Two Numbers

In this C program, we will learn about how to read two numbers and find maximum numbers using if else statement. We will first take two numbers as input from user using scanf function. Then we print the maximum number on screen.

Required Knowledge

Algirithm to find Maximum of Two Numbers

  • The algorithm begins by taking two numbers, num1 and num2, as input.

  • It then compares the two numbers using an if statement.

  • If num1 is greater than num2, it assigns the value of num1 to the variable max.

  • If num2 is greater than num1, it assigns the value of num2 to max.

  • Finally, it prints the value of max, which is the maximum of the two numbers.


Tips for Writing Programs to Find the Maximum of Two Numbers in C

  • Understand the Problem : Before starting to write the program, make sure you understand the problem statement clearly. Ensure you know what is expected as the output and the constraints of the problem.

  • Use Appropriate Variables : Use meaningful variable names such as num1, num2, and max to improve readability and understanding of the code.

  • Input Validation : Check for valid input, such as ensuring that the input is within the valid range and is of the correct data type

  • Algorithm Selection : Choose the appropriate algorithm for finding the maximum of two numbers. In this case, a simple if-else statement is sufficient.

  • Logical Flow : Ensure that the logic of your program is correct. Double-check the conditions and the order of operations to find the maximum number.

  • Code Readability : Write clean and well-formatted code. Use indentation, proper spacing, and meaningful comments to make your code easy to read and understand.

  • Testing : Test your program with different sets of input, including edge cases, to ensure that it works correctly under all conditions.

  • Error Handling : Implement error handling to deal with unexpected situations, such as invalid input or runtime errors.

  • Understanding Precedence : Understand the precedence of operators in C, such as > (greater than) and = (assignment), to ensure that your comparisons are correct.

C program to find maximum of two numbers using If Else statement

#include <stdio.h>  
  
int main() {  
    int a, b;   
    printf("Enter Two Integers\n");  
    scanf("%d %d", &a, &b);  
   
    if(a > b) {
        /* a is greater than b */
        printf("%d is Largest\n", a);          
    } else if (b > a){ 
        /* b is greater than a*/ 
        printf("%d is Largest\n", b);  
    } else {
        printf("Both Equal\n");
    }
  
    return 0;  
} 
Output
Enter Two Integers
3 6
6 is Largest
Enter Two Integers
7 2
7 is Largest
Enter Two Integers
5 5
Both Equal

C program to find maximum of two numbers using ternary operator

#include <stdio.h>  
  
int main() {  
    int a, b, max;  
 
    printf("Enter Two Integers\n");  
    scanf("%d %d", &a, &b);  
   
    if(a == b){
        printf("Both Equal\n");
    }
    
    max = (a > b) ? a : b ;
    printf("%d is Largest\n", max);
   
    return 0;  
} 
Output
Enter Two Integers
5 6
6 is Largest
Enter Two Integers
5 5
Both Equal

Conclusion

Understanding the algorithm of finding the maximum of two numbers is essential for beginners in programming. It lays the groundwork for understanding more complex algorithms and problem-solving techniques. Through practice and application, students can master this fundamental concept and apply it to various programming scenarios.

Related Topics
C Program to Find Largest of Three Numbers
C Program to Add Two Numbers
C program to add digits of a number
C Program to Find Maximum of Two Numbers using Switch Case Statement
C Program to Find Maximum of Two Numbers using Conditional Operator
C Program to Swap Two Numbers
C program to find hcf and lcm of two numbers
C Program to Add Two Complex Numbers
C Program to Calculate Factorial of a Number
List of all C programs