C Program to Find the Largest Number Among Three Numbers

Source Code 1


/* C program to find largest number using if statement only */

#include <stdio.h>
int main(){
float a, b, c;
printf
("Enter three numbers: ");
scanf
("%f %f %f", &a, &b, &c);
if(a>=b && a>=c)
printf
("Largest number = %.2f", a);
if(b>=a && b>=c)
printf
("Largest number = %.2f", b);
if(c>=a && c>=b)
printf
("Largest number = %.2f", c);
return 0;
}

Source Code 2


/* C program to find largest number using if...else statement */

#include <stdio.h>
int main(){
float a, b, c;
printf
("Enter three numbers: ");
scanf
("%f %f %f", &a, &b, &c);
if (a>=b)
{
if(a>=c)
printf
("Largest number = %.2f",a);
else
printf
("Largest number = %.2f",c);
}
else
{
if(b>=c)
printf
("Largest number = %.2f",b);
else
printf
("Largest number = %.2f",c);
}
return 0;
}

Source Code 3


/* C Program to find largest number using nested if...else statement */

#include <stdio.h>
int main(){
float a, b, c;
printf
("Enter three numbers: ");
scanf
("%f %f %f", &a, &b, &c);
if(a>=b && a>=c)
printf
("Largest number = %.2f", a);
else if(b>=a && b>=c)
printf
("Largest number = %.2f", b);
else
printf
("Largest number = %.2f", c);
return 0;
}
Though the technique to solve this problem is different in these three examples, output of all these program is same.

Enter three numbers: 12.2
13.452
10.193
Largest number = 13.45

0 comments:

Post a Comment

My Instagram