C++ Program to find the Greatest of three numbers

Program to find the Greatest of Three Numbers

Here we will discuss how to find the greatest of three numbers entered by the user in c++ programming language.

To find the greatest of three numbers if else-if statement is used along with ‘>’ ad ‘&&’ operator in the condition. 

Working

  • User gives three inputs.
  • The inputs are stored in three int type variables say first, second and third.
  • The inputs are then compared using the if else if statements   
  • If the below stated statement is true then first is the greatest.

                             if((first > second) &&(first > third))

  • Otherwise another conditioned is checked which gives second as the greatest.

                             else if((second > first) && (second > third))

  • Else third is the greatest of the three.
Program to find Greatest of three numbers

C++ Code

//C++ Program
//Greatest of three numbers
#include<iostream>
using namespace std;
//main program
int
main()
{
int first, second, third;
cout<<"Enter first number: ";
cin>>first;
cout<<"Enter second number: ";
cin>>second;
cout<<"Enter third number: ";
cin>>third;
//comparing first with other numbers
if((first >= second) && (first >= third))
{
cout<<first<<" is the greatest";
}
//comparing Second with other numbers
else if((second >= first) && (second >= third))
{
cout<<second<<" is the greatest";
}
else
{
cout<<third<<" is the greatest";
}
return 0;
}

Output

Enter first number: 97
Enter second number: 18
Entered third number: 6
97 is the greatest

Please Login/Signup to comment