The biggest of the three numbers in C
In this tutorial, we will see the solutions for finding the biggest of the three numbers in C programming using different methods.
Let us first have a look at this example; among the three numbers 87, 23, and 56, the biggest number is 87.
87 23 56
So, now we will see some programs to find the biggest number among the three.
How to Determine the largest number among the three numbers
- Making use of if-else statements
- Applying nested if-else statements
- The use of temporary variable
- Using if-else statements, find the largest number.
The goal is to determine which of the three numbers is the maximum by comparing it with the other two using the compound expression.
Algorithm:
- If A, B, and C are equal, print "all are equal" otherwise
- If A is bigger than or equal to B and C, it is the largest number.
- Make sure that B is the largest number by confirming that it is greater than or equal to both A and C.
- If you can confirm that C is larger than or equal to A and B, then that number is C.
C Program to Use an If-else Statements to Find the Biggest Number
#include <stdio.h>
int main()
{
int P, Q, R;
printf("Input three numbers P, Q and R: ");
scanf("%d %d %d", &P, &Q, &R);
// Using compound expressions to find max
if (P == Q && Q == R)
printf("All numbers are equal.");
else if (P >= Q && P >= R)
printf("%d is the largest number.", P);
else if (Q >= P && Q >= R)
printf("%d is the largest number.", Q);
else
printf("%d is the largest number.", R);
return 0;
}
Using if-else statements and compound expressions, this C program takes three user-supplied numbers (P, Q, and R) and prints the largest. In addition, it prints a message by the three numbers' equality.
Output:
Analysis of Complexity
O(1) time complexity because it performs continuous operations
Space auxiliary: O(1)
2. Applying a Nested if-else statement, find the largest number.
In contrast to the earlier approach, we only consider one condition per if statement and nestle the remaining conditions inside it as if-else statements.
C Program to Use nested If-Else to Find the Biggest Number
#include <stdio.h>
int main()
{
int P, Q, R;
// Input three numbers
printf("Input any three numbers P, Q, and R: ");
scanf("%d %d %d", &P, &Q, &R);
// Using nested if-else to find the largest number
if (P > Q) {
if (P > R)
printf("%d is the largest number.", P);
else if (P == R)
printf("%d and %d are the largest numbers (equal).", P, R);
else
printf("%d is the largest number.", R);
} else if (P > R) {
if (P == Q)
printf("%d and %d are the largest numbers (equal).", P, Q);
else
printf("%d is the largest number.", Q);
} else if (R == P)
printf("%d and %d are the largest numbers (equal).", P, R);
else
printf("%d is the largest number.", R);
return 0;
}
This C program uses nested if-else statements to take three input numbers (P, Q, and R) and output the largest number. It includes the largest, checks for equality, and prints messages when two or more numbers are equal. Information about the greatest number and possible equality among them is provided in the output.
Output:
Analysis of Complexity
O(1) time complexity because it performs continuous operations
Space auxiliary: O(1)
3. Using a Temporary Variable, find the largest number
Using this method, we designate a temporary variable to hold the number that we assume to be the maximum. Next, we compare this temporary variable, one by one, with the other two numbers; if the comparison is smaller than the max, the comparison is assigned to the max.
Write a C program using a temporary variable to determine which of three numbers is the largest.
// C Programme to Use Temporary Variable to Find
// the Largest Number Among Three
#include <stdio.h>
int main()
{
int P, Q, R;
printf("Input any three numbers P, Q, and R: ");
scanf("%d %d %d", &P, &Q, &R);
int MAX = P;
if (MAX < Q)
MAX = Q;
if (MAX < R)
MAX = R;
printf("Maximum among %d, %d, and %d is: %d", P, Q, R, MAX);
return 0;
}
This C program takes three input numbers (P, Q, and R) and finds and prints the largest number using if statements and a temporary variable called `MAX`. The highest value among the entered numbers is shown in the output.
Output: