C programming
#include <stdio.h>
Try it Yourself ➠
#include <stdio.h>
Try it Yourself ➠
#include <stdio.h>
Try it Yourself ➠
#include<stdio.h>
Try it Yourself ➠
Greatest among three numbers in C
02 July
0
Write a program (WAP) in C to read three numbers from keyboard(User Input) and find-out the greatest among three numbers.
1. Using Simple if :-
#include <stdio.h>
int main()
{
float n1, n2, n3;
printf("Enter three numbers :\n ");
scanf("%d%d%d",n1,n2,n3);
if(n1 >= n2 && n1 >= n3)
{
printf("Largest number is : ",n1);
}
if(n2 >= n1 && n2 >= n3)
{
printf("Largest number is : ",n2);
}
if(n3 >= n1 && n3 >= n2)
{
printf("Largest number is : ",n3);
}
return 0;
}
Try it Yourself ➠
2. Using if...else :-
#include <stdio.h>
int main()
{
float n1, n2, n3;
printf("Enter three numbers :\n ");
scanf("%d%d%d",n1,n2,n3);
if((n1 >= n2) && (n1 >= n3))
printf("Largest number is : ",n1);
else if ((n2 >= n1) && (n2 >= n3))
printf("Largest number is : ",n2);
else
printf("Largest number is : ",n3);
return 0;
}
Try it Yourself ➠
3. Using Nested if...else :-
#include <stdio.h>
int main()
{
float n1, n2, n3;
printf("Enter three numbers :\n ");
scanf("%d%d%d",n1,n2,n3);
if (n1 >= n2)
{
if (n1 >= n3)
printf("Largest number is : ",n1);
else
printf("Largest number is : ",n3);
}
else
{
if (n2 >= n3)
printf("Largest number is : ",n2);
else
printf("Largest number is : ",n3);
}
return 0;
}
Try it Yourself ➠
4. Using Ternary Operator :-
#include<stdio.h>
int main()
{
int n1,n2,n3,large;
printf("Enter any three numbers:\n");
scanf("%d%d%d",n1,n2,n3);
large=(n1>n2)?(n1>n3?n1:n3):(n2>n3?n2:n3);
printf("Largest number: ",large);
return 0;
}
Try it Yourself ➠
Must Read :-
Previous article
Next article
Leave Comments
Post a Comment