C Program Find Largest Among Three Numbers

C Program to Find Largest Among Three Numbers

In this tutorial, you will learn how to write a C program to find the largest/maximum among three numbers.

Our problem statement

Here we have to take three integer inputs from the user. Three input integers are ‘a’, ‘b’, and ‘c’. Now we have to compare and find the maximum number among the three given inputs. Print the output as a maximum/largest number.

For Example:

Input integers are: 77, 88, 99

Output: 99

Here Output is 99 as this is the largest/maximum number among the given three inputs.

Here we have taken three integer inputs ‘a, ‘b’, and ‘c’, post execution of the below codes, we shall get the largest/maximum number as our output.

There are multiple ways to approach this program,

  • if-else statement.
  • ternary operator.
  • a user-defined function.

Program 1: Find the largest/maximum by using if-else.

The below program is written in C using if else to find the maximum number among three. Along with that, our integer is num1, num2, and num3 whose value is taken from the user as input. After comparison using if-else, the below program will print an integer number as an output. That output will be the maximum number.

#include<stdio.h>
int main()
{
    int num1, num2, num3;
    printf("Enter three numbers: ");
    scanf("%d%d%d", &num1, &num2, &num3);

    if (num1 > num2) {
        if (num1 > num3) {
            printf("%d is the largest number.", num1);
        } else {
            printf("%d is the largest number.", num3);
        }
    } else {
        if (num2 > num3) {
            printf("%d is the largest number.", num2);
        } else {
            printf("%d is the largest number.", num3);
        }
    }
    return 0;
}

Output:

Enter three numbers: 88
77
99
99 is the largest number.

Program 2: Find the largest/maximum by using a ternary operator

In the above program, if-else statements were used. But in this program, we are using the ternary operator. The ternary operator is a conditional operator similar to the if else statement.

#include<stdio.h>
int main()
{
    int num1, num2, num3;
    printf("Enter three numbers: ");
    scanf("%d%d%d", &num1, &num2, &num3);

    int max = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3);
    printf("%d is the largest number.", max);

    return 0;
}

Output:

Enter three numbers: 77
88
99
99 is the largest number.

Program 3: Find the largest/maximum by using a user-defined function.

Another approach for this problem statement is to use a user-defined function. A UDF is a custom block of code that is written to achieve a custom goal, in our scenario to find a maximum/largest number.

#include<stdio.h>
int max(int x, int y, int z)
{
    int max = x;
    if (y > max) max = y;
    if (z > max) max = z;
    return max;
}
int main()
{
    int num1, num2, num3;
    printf("Enter three numbers: ");
    scanf("%d%d%d", &num1, &num2, &num3);

    int result = max(num1, num2, num3);
    printf("%d is the largest number.", result);

    return 0;
}

Output:

Enter three numbers: 77
88
99
99 is the largest number.