Greatest among three numbers in C++ - 👑 सार्थक मुंड S3 🤴🏻

Greatest among three numbers in C++

greatest among three numbers in c++

Write a program (WAP) in C++ to read three numbers from keyboard(User Input) and find-out the greatest among three numbers.

1. Using Simple if :-

#include <iostream>
using namespace std;
int main()
{
  float n1, n2, n3;
  cout<< "Enter three numbers :\n ";
  cin>> n1 >> n2 >> n3;
  if(n1 >= n2 && n1 >= n3)
  {
   cout<< "Largest number is : " << n1;
  }
  if(n2 >= n1 && n2 >= n3)
  {
    cout<< "Largest number is : " << n2;
  }
  if(n3 >= n1 && n3 >= n2)
  {
    cout<< "Largest number is : " << n3;
  }
    return 0;
  }

greatest among three numbers in c++

2. Using if...else :-

#include <iostream>
using namespace std;
int main()
{
  float n1, n2, n3;
  cout << "Enter three numbers :\n";
  cin >> n1 >> n2 >> n3;
  if((n1 >= n2) && (n1 >= n3))
    cout << "Largest number is : " << n1;
  else if ((n2 >= n1) && (n2 >= n3))
    cout << "Largest number is : " << n2;
  else
    cout << "Largest number is : " << n3;
return 0;
}

greatest among three numbers in c++

3. Using Nested if...else :-

#include <iostream>
using namespace std;
int main()
{
  float n1, n2, n3;
  cout << "Enter three numbers :\n ";
  cin >> n1 >> n2 >> n3;
  if (n1 >= n2)
  {
    if (n1 >= n3)
     cout << "Largest number is : " << n1;
    else
      cout << "Largest number is : " << n3;
  }
  else
  {
    if (n2 >= n3)
      cout << "Largest number is : " << n2;
    else
      cout << "Largest number is : " << n3;
   }
return 0;
}

greatest among three numbers in c++

4. Using Ternary Operator :-

#include<iostream>
using namespace std;
int main()
 {
 int n1,n2,n3,large;
 cout<<"Enter any three numbers:\n";
 cin>>n1>>n2>>n3;
 large=(n1>n2)?(n1>n3?n1:n3):(n2>n3?n2:n3);
 cout<<"Largest number: "<<large;
 return 0;
}

greatest among three numbers in c++

Also Read :-


  1. Programs of C

  2. Programs  of C++

  3. Programs of Java

  4. Programs of Python

  5. Codes with HTML, CSS, JS

  6. Questions & Answers

Previous article
Next article

Leave Comments

Post a Comment

Articles Ads

Articles Ads 1

Articles Ads 2

Advertisement Ads