C Program to Find Largest of Three Numbers

Write a C program to read three numbers and find largest of three numbers using if else statement

Required Knowledge

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


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

It first finds largest of first two numbers and then compares it with third number.

/** 
 * C program to find maximum of three numbers using 
 * if else statement
 */  
#include <stdio.h>  
  
int main()  {  
    int a, b, c, max;  
    /* 
     * Take three numbers as input from user 
     */  
    printf("Enter Three Integers\n");  
    scanf("%d %d %d", &a, &b, &c);  
    
    if(a > b){
        // compare a and c
        if(a > c)
            max = a;
        else 
            max = c;
    } else {
 // compare b and c
        if(b > c)
            max = b;
        else 
            max = c;
    }
  
    /* Print Maximum Number */  
    printf("Maximum Number is = %d\n", max);  
  
    return 0;  
}

Output
Enter Three Integers
2 8 4
Maximum Number is = 8

C program to find largest of three numbers using function

Function getMax takes two numbers as input and returns the largest of two numbers. We will use this function to find largest of three numbers as follows:

/** 
 * C program to find maximum of three numbers using
 * function operator 
 */  
#include <stdio.h>  

/*
 *It returns Maximum of two numbers
 */
int getMax(int num1, int num2) {
    if (num1 > num2){
        return num1;
    } else {
        return num2;
    }
}

int main()  {  
    int a, b, c, max;  
    /* 
     * Take three numbers as input from user 
     */  
    printf("Enter Three Integers\n");  
    scanf("%d %d %d", &a, &b, &c);  
    
    max = getMax(getMax(a, b), c);
  
    /* Print Maximum Number */  
    printf("Maximum Number is = %d\n", max);  
  
    return 0;  
}

Output
Enter Three Integers
32 45 87
Maximum Number is = 87

Related Topics
C program to find maximum of three numbers using conditional operator
C program to find maximum of two numbers using conditional operator
C program to find maximum of two numbers using switch statement
C program to check whether three angles of triangle is valid or not
C program to check whether a character is alphabet or digit
C program to reverse a number using recursion
C program to find gcd of two numbers
C program to check armstrong number
C program to print all prime numbers between 1 to N
List of all C programs