# C Program to Find Largest of Three Numbers

Here we will write a C program to find the largest of three numbers. To solve this problem if-else statement will be used.

Prerequisites for finding the largest number among three numbers:-

## C program to find the largest of three numbers using nested if-else statements

``````#include<stdio.h>
int main()
{
float num1, num2, num3;

printf("Enter three numbers: ");
scanf("%f %f %f",&num1,&num2,&num3);

if(num1 >= num2)
{
if(num1 >= num3)
printf("Largest Number=%.2f\n",num1);

else
printf("Largest Number=%.2f\n",num3);
}

else
{
if(num2>=num3)
printf("Largest Number=%.2f\n",num2);

else
printf("Largest Number=%.2f\n",num3);
}

return 0;
}``````

Output:-

Enter three numbers: `12.5 10 5.9`
Largest Number=12.50

Let num1, num2, and num3 are three variables that will store the value of three numbers. First, we compare num1 with num2, if num1 is greater than we will compare it with num3. Using this comparison we find num1 is the largest number or the num3. In the previous comparison, if num1 is less than num2 then the next comparison takes place with num2 and num3. Finally, we got the Largest Number among the three numbers.

## Using AND (&&) operator

The above program also can be written as below program. The structure of nested if-else statements in the below program is different from the previous program. This method is a short form of the above method. Here we will use AND (&&) operator.

``````#include<stdio.h>
int main()
{
int num1, num2, num3;

printf("Enter three numbers: ");
scanf("%d %d %d",&num1,&num2,&num3);

if(num1>=num2 && num1>=num3)
printf("Largest=%d\n",num1);
else if(num2>=num1 && num2>=num3)
printf("Largest=%d\n",num2);
else
printf("Largest=%d\n",num3);

return 0;
}``````

In this program, AND operator is used. When two numbers are greater than equal to the third number then we can say, that number is the biggest among them.

## C program to find the largest of three numbers using if statements

`````` #include<stdio.h>
int main()
{
int num1, num2, num3, max;

printf("Enter three numbers: ");
scanf("%d %d %d",&num1,&num2,&num3);

max = num1;
if(num2>max) max = num2;
if(num3>max) max = num3;

printf("Largest number = %d", max);

return 0;
}``````

Output:-

Enter three numbers: `10 20 50`
Largest number = 50

In this program, we assigned the first entered number to a variable max. Now, we will compare the value of max with the remaining two numbers. If the second number is bigger than the value of max, then the value of the second number will be assigned to the variable max. Similarly, the same logic applied to the third number. Finally, we got the largest number among them.

## Largest among three numbers using the conditional operator

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

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

max = (a>b && a>c)? a: (b>a && b>c)? b:c;

printf("Maximum number = %.2f",max);

return 0;
}``````

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!