c - Chaining of Relational operators is giving wrong output -


can explain me? did wrong? when run program doesn't show me right answer.

ex : when type weight = 50 kg , height = 130 cm answer should

"your bmi 29.58. overweight2.you have chance cause high blood pressure , diabetes need control diet. , fitness."

but answer shows

"your bmi 29.58. normal......"


#include<stdio.h> #include<conio.h>  int main() {   float weight, height, bmi;    printf("\nwelcome program");    printf("\nplease enter weight(kg) :");   scanf("%f", &weight);    printf("\nplease enter height(cm) :");   scanf("%f", &height);    bmi=weight/((height/100)*(height/100));    if(bmi<18.50) {      printf("your bmi : %.2f",bmi);     printf("you underweight.you should eat quality food , sufficient amount of energy , exercise proper.");   } else if(18.5<=bmi<23) {     printf("your bmi : %.2f \nyou normal.you should eat quality food , exercise proper.",bmi);   } else if(23<=bmi<25) {     printf("your bmi : %.2f \nyou overweight1 if have diabetes or high cholesterol,you should lose weight body mass index less 23. ",bmi);   } else if(25<=bmi<30) {     printf("your bmi : %.2f \nyou overweight2.you have chance cause high blood pressure , diabetes need control diet. , fitness.",bmi);   } else if(bmi>=30) {     printf("your bmi : %.2f \nyou obesity.your risk of diseases accompany obesity.you run risk of highly pathogenic. have control food , serious fitness.",bmi);   } else {     printf(" please try again! ");   }    return 0;   getch(); } 

in code, you've tried

 else if(18.5<=bmi<23) 

no, kind of chaining of relational operators not possible in c. should write

 else if((18.5<=bmi) &&  (bmi < 23)) 

to check bmi value in [18.5, 23) , on other cases.


edit:

just elaborate issue, expression like

18.5<=bmi<23 

is valid c syntax. however, same as

((18.5<=bmi) < 23 ) 

due operator associativity.

so, first (18.5<=bmi) evaluated , result , (0 or 1) gets comapred against 23, not want here.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -