Friday, 21 September 2018

Comparing three numbers and displaying the maximum number in C++

Comparing three numbers


Comparing simply means finding out the greatest among input numbers. A number becomes the greatest, only if it is greater than the other two numbers.



Write a program in C++ to find out the maximum number out of three input numbers and display it.


Here we will input and store the three values in three different integer type variables. Then we will compare one number with the other two. A greatest among three will be displayed only if it is greater than the other two. If all are equal then they all are equal message will be displayed. If none of
the condition satisfies then error message will be displayed.


Program


#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
cout<<"Enter 1st number=";
cin>>a;
cout<<"\nEnter 2nd number=";
cin>>b;
cout<<"\nEnter 3rd number=";
cin>>c;
if ((a>b)&&(a>c))
cout<<"\nThe greatest number is= "<<a;
else if ((b>c)&&(b>a))
cout<<"\nThe greatest number is= "<<b;
else if ((c>b)&&(c>a))
cout<<"\nThe greatest number is= "<<c;
else if((a==b)&&(a==c))
cout<<"\nAll three numbers are equal";
else
cout<<"\nPlease input valid numbers";
getch();
}


Output


Finding the greatest of the three numbers
DISPLAYING THE GREATEST NUMBER OUT OF THREE











Explanation


In the program, we stored the entered value in a,b and c integer type variables. Then we compared
one with other two for all cases. After comparison, we get the greatest number which is displayed on the output screen. The third condition ((c>b)&&(c>a)) of if......else if statement is satisfied for our input (c=67), so the third number was displayed on the screen. 






No comments:

Post a Comment