C Program to Find the Largest Number Among Three Numbers

CodingTute

C Program to Find the Largest Number Among Three Numbers

In this example, you will learn to find the largest number among the three numbers.

In example 1 we will find the largest number using the if-else statement and in example 2 we will find the largest number using the ternary operator.

Example 1: Find the largest number among three numbers using if-else statements

#include <stdio.h>

int main() {
    int a, b, c;
    
    printf("Enter three numbers: ");
    scanf("%d %d %d",&a,&b,&c);
    
    if(a>=b && a>=c)
        printf("%d is the largest.", a);
    else if(b>=c)
        printf("%d is the largest.", b);
    else
        printf("%d is the largest.", c);

   return 0;
}

Example 2: Find the largest number among three numbers using ternary operator

#include <stdio.h>
int main()
{
	int a, b, c, large;

	printf("Enter three numbers: ");
	scanf("%d %d %d", &a, &b, &c);

	large = a > b ? (a > c ? a : c) : (b > c ? b : c);

	printf("%d is the largest.", large);

	return 0;
}

Output

Enter three numbers: 2 3 9
9 is the largest.

Explanation

Firstly we read input from the user and store them in variables a, b and c.

We will check a is larger than b and c or not. If a is larger than the other two numbers then we will print a is largest.

If a is not larger, then we will check b is larger than c or not. If b is larger, then we will print b is largest. If b is not larger than c, then will print c is largest.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.