In C Programming, there are many approaches to find the largest number of the three numbers. Let me show you how to write a C program to find the largest of three numbers using Else If Statement, Nested If, and Conditional Operator.
C Program to find Largest of Three numbers using Else If Statement
This program helps the user to enter three different values. Then this C program find the largest number among three numbers using Else If Statement in C
/* 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 of the C Else If Statement
- In this greatest of three numbers in c example, First if condition checks whether a is greater than b and a is greater than c. if this condition is True, a is greater than both b, c.
- The first Else if condition check whether b is greater than a and b is greater than c. If this condition is True, b is greater than both a, c.
- The second Else if condition check whether c is greater than a, and c is greater than b. If this condition is True, 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
3rd OUTPUT: Lets enter the values a = 45, b = 36, c= 96
OUTPUT 4: Lets enter a = 5, b = 5, c= 5
C Program to find Largest of Three numbers using Nested If Statement
This greatest of three numbers in c program helps the user to enter three different values. Next, this C program to Find Largest of Three numbers using Nested If in C .
/* 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
In this C program to find largest of three numbers example
- First, if condition check whether a-b is greater than 0 and a-c is greater than 0. If we subtract a small number from a 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 C Programming 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
2nd OUTPUT: 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.Next, C Program find the largest among three numbers using Conditional Operator
/* 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
Within this C program to find the greatest of three numbers example
- We declared an integer variable called the largest, and we assigned this variable to the conditional operator functionality.
- The first condition checks whether a is greater than b and a is greater than c. If this condition is True, it returns first value after the ? symbol, which is variable a (a is greater than both b, c).
- If the first condition fails, it executes the variable after the : symbol. By using the Nested conditional operator, we are checking one more condition (b >c). If this condition is True, it returns the first value after the ? symbol, which is variable b (b is greater than both a, c.
- If the Nested condition fails, returns the variable after the : symbol, which is variable c. It means c is greater than both a, b.
OUTPUT 1: Lets enter the values a = 56, b = 9, c= 26 for this C Program to Find Largest of Three numbers
OUTPUT 2: a = 12, b = 48, c= 39
3rd OUTPUT: a = 2, b = 56, c= 98