+ 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 !
16 Answers
+ 4
hey bro you have to write float data type instead of int then it will work ...
+ 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++
+ 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.
+ 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))
{}
+ 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
+ 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.
0
antik bro its not working
0
instead of writing Int write float
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!
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
0
you are fail is the answer since you mentioned int a it takes value of a as 16
0
type if(19 >a && a> 15)
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.
0
int is for integers not for decimal point
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.
0
int type cannot have decimal point