+ 2

why it is not working properly?

#include<iostream> using namespace std; int main() { int a,b,c; cin>>a>>b>>c; if(a>b&&b>c) cout<<"a is greatest"<<endl; if(b>a&&a>c) cout<<"b is greatest"<<endl; if(c>a&&a>b) cout<<"c is greatest"<<endl; }

31st May 2017, 9:48 AM
Dheeraj Tailor
Dheeraj Tailor - avatar
4 Answers
+ 11
How is it not working properly? One thing I can see is that it doesn't necessarily cover all the conditions. Let's say you input 1, 2 and 3 into a, b and c respectively - c is greater than a, but a is not greater than b, hence, nothing gets output. This means that your code contains logical errors which need to be rethinked. I suggest: #include<iostream> using namespace std; int main() { int a,b,c; cin>>a>>b>>c; if(a>b&&a>c) cout<<"a is greatest"<<endl; if(b>a&&b>c) cout<<"b is greatest"<<endl; if(c>a&&c>b) cout<<"c is greatest"<<endl; }
31st May 2017, 9:50 AM
Hatsy Rei
Hatsy Rei - avatar
+ 10
@Hatsy preceded me, again! :(
31st May 2017, 9:53 AM
Maz
Maz - avatar
+ 2
Just to post an improved version of the code, ... if(a>b && a>c) cout << "a is greatest"; else if(b>c) cout << "b is greatest"; else cout << "c is greatest"; ... Hope that helps. Thanks.
31st May 2017, 10:17 AM
Patel
0
thanks @ Hasty Rei and patel
31st May 2017, 10:24 AM
Dheeraj Tailor
Dheeraj Tailor - avatar