+ 3

how can I find the largest number from three numbers

14th Jun 2016, 5:22 PM
Abdulai Torfik
6 Réponses
+ 1
int a,b,c,grt; ((a>b)&&(a>c))?grt=a:(b>c)?grt=b:grt=c;
14th Jun 2016, 5:42 PM
Parvesh Chaudhary
Parvesh Chaudhary - avatar
+ 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>)
14th Jun 2016, 6:59 PM
CHIRANJEEV
CHIRANJEEV - avatar
+ 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";
15th Jun 2016, 3:31 AM
p@w@n
p@w@n - avatar
+ 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.
15th Jun 2016, 6:08 PM
Stefan
Stefan - avatar
+ 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; }
20th Jun 2016, 7:10 PM
Alejandro Morales
Alejandro Morales - avatar
- 1
function max(a, b) { (a >= b) return ; return b; }
6th Dec 2016, 11:44 PM
Paulo Freitas
Paulo Freitas - avatar