 getting wrong answer plz help | SoloLearn: Learn to code for FREE!

0

# getting wrong answer plz help

#include <iostream> using namespace std; int main() { int a=5 ,b=6 ,c=8; if (a>(b>c)) { if(b>(c>a)) { cout << "b is greater"; } else { cout << "a is greater"; } } else { if (c>(a>b)) { cout << "c is greater"; } else { cout << "Something's wrong"; } } return 0; } //output says b is greater

+3

if( >( > )) Don't use these. Inside the second bracket, it is seeing whether or not b is greater than c. It is not, so it evaluates to false (or 0), a is then greater than 0, so the if statement evalutates to true. Again, the second bracket of the second if statement is checking whether or not c is greater than a, which is true (or 1), c is greater than 1, so the if statement evaluates to true, printing out that b is the greatest.

+1

#include <iostream> using namespace std; int main() { int a=5 ,b=6 ,c=8; if (a>b) { if(a>c) { cout << "a is greater"; } else { cout << "c is greater"; } } else { if (b>c){ cout << "b is greater"; } else { cout << "c is greater"; } } return 0; } //output says c is greater

+1

You are not using the right logical operators... When you use (a>(b>c)) the compiler first check if b>c, which in your code it's false and then checks if a>false which is true as false it's equal to 0 (zero)... I suppose that you really meant "if both a and b are greather than c".. in order to do that you should use (a>c)&&(b>c)... && it's the logical and operator in C++ Hope it helped...

+1

#include <iostream> using namespace std; int main() { int a=20,b=9,c=7; if((a>b)&&(a>c)) { cout<<"a is greater"; } else if((b>c)&&(b>a)) { cout<<"b is greater" ; } else { cout<<"c is greater" ; } return 0; } //same program with logical operator i did well didnt i???

0