Find the Largest Number Using if-else Statements
The idea is to use the compound expression where each number is compared with the other two to find out which one is the maximum of them.
Algorithm
- Check if A is greater than or equal to both B and C, A is the largest number.
- Check if B is greater than or equal to both A and C, B is the largest number.
- Check if C is greater than or equal to both A and B, C is the largest number.
C Program to Find the Largest Number Using if Statement
C
// C program to find the maximum number out of the three // given numbers using if-else statement #include <stdio.h> int main() { int A, B, C; printf ( "Enter the numbers A, B and C: " ); scanf ( "%d %d %d" , &A, &B, &C); // finding max using compound expressions if (A >= B && A >= C) printf ( "%d is the largest number." , A); else if (B >= A && B >= C) printf ( "%d is the largest number." , B); else printf ( "%d is the largest number." , C); return 0; } |
Output
Enter the numbers A, B and C: 32764 is the largest number.
Complexity Analysis
- Time complexity: O(1) as it is doing constant operations
- Auxiliary space: O(1)
C program to Find the Largest Number Among Three Numbers
Here, we will learn how to find the largest number among the three numbers using the C program. For example, the largest number among 2, 8, and 1 is 8.