CPP
#include <iostream>
#include <iostream>
#include <iostream>
#include<iostream>
Greatest among three numbers in C++
02 July
0
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;
}
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;
}
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;
}
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;
}
Also Read :-
Programs of C
Programs of C++
Programs of Java
Programs of Python
Codes with HTML, CSS, JS
Questions & Answers
Previous article
Next article
Leave Comments
Post a Comment