Example 1: Using if Statement
#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
// if n1 is greater than both n2 and n3, n1 is the largest
if (n1 >= n2 && n1 >= n3)
printf("%.2f is the largest number.", n1);
// if n2 is greater than both n1 and n3, n2 is the largest
if (n2 >= n1 && n2 >= n3)
printf("%.2f is the largest number.", n2);
// if n3 is greater than both n1 and n2, n3 is the largest
if (n3 >= n1 && n3 >= n2)
printf("%.2f is the largest number.", n3);
return 0;
}
Here, we have used 3 different if
statements. The first one checks whether n1 is the largest number.
The second and third if
statements check if n2 and n3 are the largest, respectively.
The biggest drawback of this program is that all 3 if
statements are executed, regardless of which number is the largest.
However, we want to execute only one if
statement. We can do this by using an if...else
ladder.
Example 2: Using if...else Ladder
#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
// if n1 is greater than both n2 and n3, n1 is the largest
if (n1 >= n2 && n1 >= n3)
printf("%.2lf is the largest number.", n1);
// if n2 is greater than both n1 and n3, n2 is the largest
else if (n2 >= n1 && n2 >= n3)
printf("%.2lf is the largest number.", n2);
// if both above conditions are false, n3 is the largest
else
printf("%.2lf is the largest number.", n3);
return 0;
}
In this program, only the if
statement is executed when n1 is the largest.
Similarly, only the else if
statement is executed when n2 is the largest, and so on.
Example 3: Using Nested if...else
#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
// outer if statement
if (n1 >= n2) {
// inner if...else
if (n1 >= n3)
printf("%.2lf is the largest number.", n1);
else
printf("%.2lf is the largest number.", n3);
}
// outer else statement
else {
// inner if...else
if (n2 >= n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
}
return 0;
}
In this program, we have used nested if...else
statements to find the largest number. Let's see how they work in greater detail.
1. Outer if Statement
First, notice the outer if
statement and the inner if...else statement inside it:
// outer if statement
if (n1 >= n2) {
// inner if...else
if (n1 >= n3)
printf("%.2lf is the largest number.", n1);
else
printf("%.2lf is the largest number.", n3);
}
Here, we are checking if n1 is greater than or equal to n2. If it is, the program control goes to the inner if...else
statement.
The inner if
statement checks whether n1 is also greater than or equal to n3.
If it is, then n1 is either equal to both n2 and n3, or it is now greater than both n2 and n3 i.e. n1 >= n2 >= n3
. Hence, n1 is the largest number.
Else, n1 is greater than or equal to n2 but it is less than n3 i.e. n3 > n1 >= n2
. Hence, n3 is the largest number.
2. Outer else Statement
The outer else
statement is executed when n2 > n1
:
// outer else statement
else {
// inner if...else
if (n2 >= n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
}
The inner if...else
of this part of the program uses the same logic as the one before. The only difference here is that we're checking if n2 is greater than n3.
The output of all these programs above will be the same.
Enter three numbers: -4.5 3.9 5.6 5.60 is the largest number.