Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

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. 

Program to find the Greatest of Three Numbers

Algorithm:-

  1. Take three numbers as inputs.
  2. The inputs are stored in three int type variables say first, second and third.
  3. The inputs are then compared using the if else if statements   
  4. If the below stated statement is true then first is the greatest.

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

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

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

      6. Else third is the greatest of the three.

C++ Code:-

    
    //C++ Program
    //Greatest of three numbers
    #include<iostream>
    using namespace std;
    //main program
    int main()
    {
        int firstsecondthird;
        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