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


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
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