+ 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; }
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;
}
+ 10
@Hatsy preceded me, again! :(
+ 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.
0
thanks @ Hasty Rei and patel