FIND GREATEST NUMBER AMONG THREE NUMBERS
In this program we learn how to find greatest number among three number provided by user using C.
Example:
Input : A = 6, B = 9, C = 11
Output: Greatest Number = 11
Input : A = 456, B = 129, C = 99
Output: Greatest Number = 456
Here we learn , to find greatest number among three numbers by using if, if-else, nested if-else and Ternary operator in C.Output: Greatest Number = 11
Input : A = 456, B = 129, C = 99
Output: Greatest Number = 456
Algorithm to find greatest number among three numbers:
Step 1: Start
Step 2: Read three numbers as A, B and C.
Step 3: Check if A is greater than B.
Step 3.1: If true, then check if A is greater than C.
Step 3.1.1: If true, then print 'A' is largest number.
Step 3.1.2: If false, then print 'C' is largest number.
Step 3.2: If false, then check if B is greater than C.
Step 3.2.1: If true, then print 'B' is largest number.
Step 3.2.2: If false, then print 'C' is largest number.
Step 4: End
Step 2: Read three numbers as A, B and C.
Step 3: Check if A is greater than B.
Step 3.1: If true, then check if A is greater than C.
Step 3.1.1: If true, then print 'A' is largest number.
Step 3.1.2: If false, then print 'C' is largest number.
Step 3.2: If false, then check if B is greater than C.
Step 3.2.1: If true, then print 'B' is largest number.
Step 3.2.2: If false, then print 'C' is largest number.
Step 4: End
FlowChart to find greatest number among three numbers:
Below the example the C program to find greatest number among three numbers.Example 1: Using only if statement to find greatest number among three numbers.// Find greatest in 3 numbers
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\n Enter three numbers : ");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("\n The greatest number : %d",a);
if(b>a && b>c)
printf("\n The greatest number : %d",b);
if(c>a && c>b)
printf("\n The greatest number : %d",c);
getch();
}
OUTPUT:
Enter three numbers: 44 22 77
The greatest number : 77
Example 2: Using if-else statement to find greatest number among three numbers.
// Find greatest in 3 numbers
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\n Enter three numbers : ");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("\n The greatest number : %d",a);
else
printf("\n The greatest number : %d",a);
}
else
{
if(b>c)
printf("\n The greatest number : %d",b);
else
printf("\n The greatest number : %d",c);
}
getch();
}
OUTPUT:
Enter three numbers: 14 2 7
The greatest number : 14
Example 3: Using neasted if-else statement to find greatest number among three numbers.
// Find greatest in 3 numbers
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\n Enter three numbers : ");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("\n The greatest number : %d",a);
else if(b>a && b>c)
printf("\n The greatest number : %d",b);
else
printf("\n The greatest number : %d",c);
getch();
}
OUTPUT:
Enter three numbers: 25 122 98
The greatest number : 122
Example 4: Using Ternary operator to find greatest number among three numbers.
// Find greatest in 3 numbers
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c,greatest;
clrscr();
printf("\n Enter three numbers : ");
scanf("%d%d%d",&a,&b,&c);
greatest = a > b ? (a>c ? a:c) : (b>c ? b:c);
printf("\n The greatest number : %d",greatest);
getch();
}
OUTPUT:
Enter three numbers: 10 8 5
The greatest number : 10
No comments:
Post a Comment