C++ Program to Find Greatest of Three Numbers

Here we will see the programs to find greatest of three number using different method. 

greatest of three number, largest number, biggest integer value

i) Program using nested if else:
#include <iostream>
using namespace std;

int main ()
{
  int a, b, c;
  cout << "Enter three numbers" << endl;
  cin >> a >> b >> c;

  if (a >= b)
    {
      if (a >= c)
	    cout << a << " is the greatest number.";

      else
	    cout << c << " is the greatest number.";
    }

  else
    {
      if (b >= c)
	    cout << b << " is the greatest number.";

      else
	    cout << c << " is the greatest number.";
    }

  return 0;
}
Output:
Enter three numbers
54
98
67
98 is the greatest number.
ii) Program using if else:
#include <iostream>
using namespace std;

int main ()
{
  int a, b, c;
  cout << "Enter three numbers" << endl;
  cin >> a >> b >> c;

  if (a >= b && a >= c)
    cout << a << " is the gratest number";

  else if (b >= a && b >= c)
    cout << b << " is the gratest number";

  else
    cout << c << " is the gratest number";

  return 0;
}
Output:
Enter three numbers
54
98
67
98 is the greatest number.
iii) Program using ternary operator:
#include <iostream>
using namespace std;

int main ()
{
  int a, b, c, max;
  cout << "Enter three numbers" << endl;
  cin >> a >> b >> c;

  max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
  cout << "Greatest number is:" << max;

  return 0;
}
Output:
Enter three numbers
54
98
67
Greatest number is:98
C++ Program to Find Greatest of Three Numbers C++ Program to Find Greatest of Three Numbers Reviewed by Beast Coder on February 27, 2021 Rating: 5

No comments:

Powered by Blogger.