C++ Program to Find the Greatest of the Three Numbers

Find the Greatest of Three Numbers in C++

Given three integers num1, num2, and num3 as inputs.

The objective is to write a code to Find the Greatest of the Three Numbers in C++ Language. To do so we’ll check the numbers with each other and print the largest of them all.

Example

Input : num1 = 2 num2 = 9 num3 = 4

Output : 9

Flow Diagram:-

Greatest of the Three numbers.jpg

Code:-

  • Declare a variable a, b, c and largest as integer;

  • Read the number a, b and c;

  • max = a > b ? (a > c ? a : c) : (b > c ? b : c);

  • print max;

Algorithm:-

Code:-

#include<iostream>
using namespace std;
int main ()
{
    int first, second, third;
    
    cout << "Enter the three numbers:";

    cin >> first >> second >> 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 a number: 10 15 20

20 is the greatest