+ 3
how can I find the largest number from three numbers
6 Réponses
+ 1
int a,b,c,grt;
((a>b)&&(a>c))?grt=a:(b>c)?grt=b:grt=c;
+ 1
If there are 3 ints a, b and c.. Then
return max(a, max(b, c)) ;
Will give you the result you seek
You'll have to include the algorithm header too to use the max function ( #include <algorithm>)
+ 1
if(a>=b && a>=c)
cout<<"a is largest ";
else if(b>=a && b>=c)
cout<<" b is largest ";
else
cout<<"c is largest";
+ 1
A "cleaner" alternative:
#include <algorithm>
... then in code...
std::max(a, std::max(b,c));
It's still quite short and most directly expresses your intent.
+ 1
#include <iostream>
using namespace std;
int main() {
//Set the numbers
int a = 8;
int b = 8;
int c = 13;
//Comparing a
if (a > b && a > c) {
cout<<"a("<<a<<") is the greatest number."<<endl;
}
else {
if (a == b) {
cout<<"a("<<a<<") equals to b."<<endl;
}
if (a == c) {
cout<<"a("<<a<<") equals to c."<<endl;
}
}
//Comparing b
if (b > a && b > c) {
cout<<"b("<<b<<") is the greatest number."<<endl;
}
else {
if (b == a) {
cout<<"b("<<b<<") equals to a."<<endl;
}
if (b == c) {
cout<<"b("<<b<<") equals to c."<<endl;
}
}
//Comparing c
if (c > a && c > b) {
cout<<"c("<<c<<") is the greatest number."<<endl;
}
else {
if (c == b) {
cout<<"c("<<c<<") equals to b."<<endl;
}
if (c == a) {
cout<<"c("<<c<<") equals to a."<<endl;
}
}
return 0;
}
- 1
function max(a, b) {
(a >= b)
return
;
return b;
}