what will be printed output , if I write this code ? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

what will be printed output , if I write this code ?

#include<iostream> using namespace std;int main(){int a=16.7; if((19>a)&&(a>16.5)){cout<<"you are pass\n";}else{ cout<<"you are fail\n";}return 0;}according to the above code it should print "you are pass" , but I am getting "you are fail "please help !

8th Jul 2016, 9:14 AM
Tushar SHAILY
Tushar SHAILY - avatar
16 Respostas
+ 4
hey bro you have to write float data type instead of int then it will work ...
11th Jul 2016, 6:15 AM
Qutier -
Qutier - - avatar
+ 2
Extremely unlikely as the ">" operator returns a bool type, i.e. one of the ">" will do as expected and then you compare the bool result of the first ">" test with the number in the second ">" test. Still, you can express this but you have to split it like so: if ((19.5>a)&&(a>16.5)) You can test your hypothesis and my solution in the online compiler of SoloLearn C++
8th Jul 2016, 10:23 AM
Stefan
Stefan - avatar
+ 2
you have write data type for a as int. therefore a=16 hence if((a<19.5)&&(a>16.5)) in this a <19. 5-true (1)statement but a>16. 5-false(0) hence 1&&0=0 (false)->according to logical operator operations hence if (0)=false, hence else condn is executed.
11th Jul 2016, 5:15 AM
Kiran Borade
Kiran Borade - avatar
+ 1
No, it does not work that way. You can only compare two values at a time. To eliminate this problem C++ offers '&&' operator. Eg code: if((a<19.5)&&(a>16.5)) {}
9th Jul 2016, 3:34 PM
Antik Guharoy
Antik Guharoy - avatar
+ 1
actually according to your code you have initialised " int a=16.7" so here int a takes value 16 only so a< 16.5 not > 16.5
12th Jul 2016, 11:21 AM
Akshay Chidura
Akshay Chidura - avatar
+ 1
bro... you have given the value of a in integer data type whereas you should have given it in float data type. your code will only accept the whole number part i.e. 16.. change the data type.. It will work.
12th Jul 2016, 4:48 PM
Prabhu
0
antik bro its not working
9th Jul 2016, 4:30 PM
Tushar SHAILY
Tushar SHAILY - avatar
0
instead of writing Int write float
9th Jul 2016, 5:11 PM
Praneethkumar Sajalu
Praneethkumar Sajalu - avatar
0
see u took int type,so a value become 16 not 16.7 so 19>16 is true but 16>16.5 is false. then whole condition becomes false. so "u r fail" will execute. if I am wrong ,plz reply me!
9th Jul 2016, 6:52 PM
Anup Shetty
0
u have written int a = 16.7 but c++ will only read it as 16 because u have used data type as integer if u wnat the code to work type if((19>a)&&(a>15)) then it will work
9th Jul 2016, 7:00 PM
Shashwat Gandhi
Shashwat Gandhi - avatar
0
you are fail is the answer since you mentioned int a it takes value of a as 16
10th Jul 2016, 6:06 PM
kasi gunda
kasi gunda - avatar
0
type if(19 >a && a> 15)
11th Jul 2016, 2:32 AM
Shashwat Gandhi
Shashwat Gandhi - avatar
0
The input number that you have given 16.5 is of type int, so compiler considers it as 16 and therefore 16>16.5 is wrong, so you should use proper data type before using a variable.
13th Jul 2016, 6:35 PM
Pramodh Kumar M
Pramodh Kumar M - avatar
0
int is for integers not for decimal point
15th Jul 2016, 2:18 AM
Siddharth Krishnan
Siddharth Krishnan - avatar
0
Basically assigning "int" to a variable will make it not read decimal points. So instead of 16.7 you get just 16. Making (a>16.5) false and therefore also falsifying the whole (19>a)&&(a>16.5) statement being the && means both arguments need to be true.
15th Jul 2016, 6:56 PM
Adam
0
int type cannot have decimal point
28th Jul 2016, 2:49 PM
Dhruva Kashyap