getting wrong answer plz help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

17th Aug 2016, 9:47 AM
Kishan Khanpara
Kishan Khanpara - avatar
5 Answers
+ 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.
17th Aug 2016, 10:51 AM
Cohen Creber
Cohen Creber - avatar
+ 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
17th Aug 2016, 10:46 AM
Amel Alrooh
Amel Alrooh - avatar
+ 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...
17th Aug 2016, 10:47 AM
Nelson Urbina
Nelson Urbina - avatar
+ 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???
19th Aug 2016, 7:13 AM
Kishan Khanpara
Kishan Khanpara - avatar
0
thanks for help
17th Aug 2016, 12:15 PM
Kishan Khanpara
Kishan Khanpara - avatar