Exercise:
Write a C Program to find the largest number among three numbers.
1. Find using if Statement
Click Here to View the Solution!
#include <stdio.h>
int main() {
double num1, num2, num3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &num1, &num2, &num3);
// if n1 is greater than both num22 and num3
if (num1 >= num2 && num1 >= num3) {
printf("%.2f is the largest number.", num1);
}
// if num2 is greater than both num1 and num3
if (num2 >= num1 && num2 >= num3) {
printf("%.2f is the largest number.", num2);
}
// if num3 is greater than both num1 and num2
if (num3 >= num1 && num3 >= num2) {
printf("%.2f is the largest number.", num3);
}
return 0;
}
Click Here to View the Output!
Enter three different numbers: 6 7 3
7.00 is the largest number.
Click Here to View the Explanation!
- This program is used to find the largest number among three numbers using the if statement.
- In the
main method
, the program requests the user to enter three numbers and store them into thedouble type
variablesnum1, num2 and num3
using thescanf()
function. - The program uses three
if statements
each of which checks whethernum1, num2 or num3
is the largest number in comparison to one another. - The largest number when found is printed and displayed on the screen.
- The
return 0 statement
is used to end the program.
2. Find using if…else ladder
Click Here to View the Solution!
#include <stdio.h>
int main() {
double num1, num2, num3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &num1, &num2, &num3);
// if num1 is greater than both num2 and num3
if (num1 >= num2 && num1 >= num3) {
printf("%.2lf is the largest number.", num1);
}
// if num2 is greater than both num1 and num3
else if (num2 >= num1 && num2 >= num3) {
printf("%.2lf is the largest number.", num2);
}
// if both conditions are false num3 is the largest
else {
printf("%.2lf is the largest number.", num3);
}
return 0;
}
Click Here to View the Output!
Enter three numbers: 5 8 2
8.00 is the largest number.
Click Here to View the Explanation!
- This program is used to find the largest number among three numbers using the
if…else statement
. - In the
main method
, the program requests the user to enter three numbers and store them into the double type variablesnum1, num2 and num3
using thescanf()
function. - Initially, the
if statement
comparesnum1
with bothnum2 and num3
and ifnum1
is larger than both then the message num1 is the largest number is printed. - If the if condition is false, the
if…else statement
is checked which comparesnum2
with bothnum1
andnum3
and ifnum2
is larger than both then the messagenum2
is the largest number is printed. - If both the first two conditions are false, the
else statement
is executed which printsnum3
is the largest number. - The
return 0 statement
is used to exit the program execution.
3. Find using Nested if…else
Click Here to View the Solution!
#include <stdio.h>
int main() {
double num1, num2, num3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &num1, &num2, &num3);
if (num1 >= num2) {
if (num1 >= num3) {
printf("%.2lf is the largest number.", num1);
}
else {
printf("%.2lf is the largest number.", num3);
}
}
else {
if (num2 >= num3) {
printf("%.2lf is the largest number.", num2);
}
else {
printf("%.2lf is the largest number.", num3);
}
}
return 0;
}
Click Here to View the Output!
Enter three numbers: 3 9 6
9.00 is the largest number.
Click Here to View the Explanation!
- This program is used to find the largest number among three numbers using the
nested if…else
statement. - In the main method, the program requests the user to enter three numbers and store them into the double type variables
num1, num2 and num3
using thescanf() function
. - The outer
if statement
checks whethernum1
is greater thannum2
and contains anotherif…else statement
which comparesnum1
withnum3
and prints thatnum1
is the largest number if both the outer and innerif statements
are true. Else, the messagenum3
is the largest number is printed by the innerelse statement
. - The outer
else statement
contains anotherif…else statement
which comparesnum2
withnum3
and prints thatnum2
is the largest number if the outerif statement
is false and innerif statement
inside the outerelse statement
is true. Else, the messagenum3
is the largest number is printed by the innerelse statement
. - The
return 0 statement
is used to exit the program execution.