Although We have many approaches to find largest number among three numbers. In this article we will show you, How to write a C program to find largest of three numbers using Else If Statement in C, Nested If in C and Conditional Operator in C.

## C Program to find Largest of Three numbers using Else If Statement

This C program helps the user to enter three different values and then it will find the largest number among that three numbers using Else If Statement in C

**CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/* C Program to find Largest of Three numbers using Else If Statement */ #include <stdio.h> int main() { int a, b, c; printf("Please Enter three different values\n"); scanf("%d %d %d", &a, &b, &c); if (a > b && a > c) { printf("\n%d is Greater Than both %d and %d", a, b, c); } else if (b > a && b > c) { printf("\n%d is Greater Than both %d and %d", b, a, c); } else if (c > a && c > b) { printf("\n%d is Greater Than both %d and %d", c, a, b); } else { printf("\nEither any two values or all the three values are equal"); } return 0; } |

**ANALYSIS**

- First if condition check whether a is greater than b and a is greater than c. if this condition is True then a is greater than both b, c.
- First Else if condition check whether b is greater than a and b is greater than c. if this condition is True then b is greater than both a, c.
- Second Else if condition check whether c is greater than a and c is greater than b. if this condition is True then c is greater than both a, b.
- If all the above conditions fail, it means they are equal.

**OUTPUT 1: **Lets enter the values a = 12, b = 4, c= 6

**OUTPUT 2: **Lets enter the values a = 19, b = 25, c= 20

**OUTPUT 3: **Lets enter the values a = 45, b = 36, c= 96

**OUTPUT 4: **Lets enter the values a = 5, b = 5, c= 5

## C Program to find Largest of Three numbers using Nested If Statement

This program helps the user to enter three different values and then it will find the largest number among that three numbers using Nested If in C .

**CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
/* C Program to find Largest of Three numbers using Nested If Statement */ #include<stdio.h> int main() { int a, b, c; printf("Please Enter three different values\n"); scanf("%d %d %d", &a, &b, &c); if(a-b>0 && a-c>0) { printf("\n%d is Greater Than both %d and %d", a, b, c); } else { if(b-c>0) { printf("\n%d is Greater Than both %d and %d", b, a, c); } else { printf("\n%d is Greater than both %d and %d", c, a, b); } } return 0; } |

**ANALYSIS**

- First if condition check whether a-b is greater than 0 and a-c is greater than 0. If we subtract small number from big number then this condition fail, otherwise it will be True. If this condition is True then a is greater than both b, c.
- Else statement will execute when the first If condition is False so, there is no need to check for a value. In the Else statement we are inserting one more if condition (Nested IF) to check whether b-c is greater than 0. If this condition is True then b is greater than both a, c.
- Else c is greater than both a, b.

**OUTPUT 1: **Lets enter the values a = 32, b = 45, c= 98

**OUTPUT 2: **Lets enter the values a = 22, b = 5, c= 7

**OUTPUT 3: **Lets enter the values a = 56, b = 222, c= 98

## C Program to find Largest of Three numbers using Conditional Operator

This program helps the user to enter three different values and then it will find the largest number among that three numbers using Conditional Operator in C

**CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/* C Program to find Largest of Three numbers using Conditional Operator */ #include<stdio.h> int main() { int a, b, c, largest; printf("Please Enter three different values\n"); scanf("%d %d %d", &a, &b, &c); largest =((a>b && a>c)?a: (b>c)?b:c); printf("\nLargest number among three is: %d", largest); return 0; } |

**ANALYSIS**

- We declared an integer variable called largest and we assigned this variable to the conditional operator functionality.
- First condition check whether a is greater than b and a is greater than c. If this condition is True then it will return first value after the
**?**symbol, which is variable a (a is greater than both b, c). - If the first condition fail then it will execute the variable after the
**:**symbol. By using Nested conditional operator we are checking one more condition here (b >c). If this condition is True then it will return first value after the**?**symbol, which is variable b (b is greater than both a, c. - If the Nested condition fails then it will execute the variable after the
**:**symbol, which is variable c. It means cis greater than both a, b.

**OUTPUT 1: **Lets enter the values a = 56, b = 9, c= 26

**OUTPUT 2: **Lets enter the values a = 12, b = 48, c= 39

**OUTPUT 3: **Lets enter the values a = 2, b = 56, c= 98

Thank You for Visiting Our Blog

Alekhya says

C program to find greatest of three numbers without using relational operator