This program will take three integer numbers from user and find largest number among them; we will find largest number using three methods:-
#include<stdio.h>
main(){
int a,b,c,big;
printf("Enter the value of a,b & c:");
scanf("%d%d%d",&a,&b,&c);
big=a>b?a>c?a:c:b>c?b:c;
printf("The biggest no. is: %d",big);
}
Output:
2. Using only if :
1. #include<stdio.h>
main(){
int a,b,c,max;
printf("Enter the value of a,b & c:");
scanf("%d%d%d",&a,&b,&c);
max=a;
if(max<b)
max=b;
if(max<c)
max=c;
printf("The biggest no. is: %d",max);
}
Output:
2. #include<stdio.h>
main() {
int a,b,c;
printf("enter the value of a, b, c:");
scanf("%d %d %d",&a,&b,&c);
if (a>b && a>c)
{
printf("The biggest no. is: %d",a);
}
if (b>a && b>c)
{
printf("The biggest no. is: %d ",b);
}
if (c>a && c>b)
{
printf("The biggest no. is: %d",c);
}}
Output:
Using Nested if-else:
#include<stdio.h>
main(){
int a,b,c;
printf("Enter the value of a,b & c:");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("The biggest no. is: %d",a);
}
else
{
printf("The biggest no. is: %d",c);
}
}
else
{
if(b>c)
{
printf("The boggest no. is: %d",b);
}
else
{
printf("The biggest no. is: %d",c);
}}}
Output:
- Using Ternary Operators
- Using only if
- Using Nested if-else
#include<stdio.h>
main(){
int a,b,c,big;
printf("Enter the value of a,b & c:");
scanf("%d%d%d",&a,&b,&c);
big=a>b?a>c?a:c:b>c?b:c;
printf("The biggest no. is: %d",big);
}
Output:
2. Using only if :
1. #include<stdio.h>
main(){
int a,b,c,max;
printf("Enter the value of a,b & c:");
scanf("%d%d%d",&a,&b,&c);
max=a;
if(max<b)
max=b;
if(max<c)
max=c;
printf("The biggest no. is: %d",max);
}
Output:
2. #include<stdio.h>
main() {
int a,b,c;
printf("enter the value of a, b, c:");
scanf("%d %d %d",&a,&b,&c);
if (a>b && a>c)
{
printf("The biggest no. is: %d",a);
}
if (b>a && b>c)
{
printf("The biggest no. is: %d ",b);
}
if (c>a && c>b)
{
printf("The biggest no. is: %d",c);
}}
Output:
Using Nested if-else:
#include<stdio.h>
main(){
int a,b,c;
printf("Enter the value of a,b & c:");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("The biggest no. is: %d",a);
}
else
{
printf("The biggest no. is: %d",c);
}
}
else
{
if(b>c)
{
printf("The boggest no. is: %d",b);
}
else
{
printf("The biggest no. is: %d",c);
}}}
Output:
3 way to Find Largest Number among three Numbers in c
Reviewed by vijay pratap singh
on
April 22, 2017
Rating:
No comments: