C Program to Find Maximum of Two Numbers

  • Write a C program to read two numbers and find maximum numbers using if else statement.
  • Wap in C to find largest number using ternary operator.

Required Knowledge

We will first take two numbers as input from user using scanf function. Then we print the maximum number on screen.

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

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

/** 
 * C program to print maximum or largest of two numbers 
 */ 
   
#include <stdio.h>  
   
int main() {  
    int a, b;  
    /* 
     * Take two integer as input from user 
     */ 
    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

C program to find maximum of two numbers using ternary operator

/** 
 * C program to print maximum or largest of two numbers 
 * using ternary Operator
 */ 
   
#include <stdio.h>  
   
int main() {  
    int a, b, max;  
    /* 
     * Take two integer as input from user 
     */ 
    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