0
Why is my C BMI code giving wrong results?
I'm currently learning about conditional statements in C. I'm working on an exercise to calculate BMI, but when I run the code, the results seem incorrect. Can you help me identify the issue? #include<stdio. h> int main() { float weight, height; scanf("%f %f", &weight, &height); float BMI = weight / (height * height); if (BMI < 18.5) { printf("Underweight"); } else if (BMI > 25.0) { printf("Overweight"); } else { printf("Normal"); } return 0; }
10 ответов
+ 1
Brian I am sure that the problem is because of incorrect input, I tried it with input (for example: 60 170) and it's working correctly
+ 2
The "Obesity" range is missing.
Also, the "Overweight" range should include BMI==25.
+ 2
Arin my mistake. I thought you were working on the BMI task in the Community Code Coach. I gather that this is the one within Introduction to C.
The difference is that in the C tutorial, you are given bmi as the input, not weight and height.
+ 1
Arin did you make the input correction that I pointed out (to read bmi instead of weight and height)?
+ 1
Diana🌷 with your correction the code would be fine if weight and height were the provided input. But in this case, Code Coach provides BMI as the input. Refer to the full description of the task copied below:
Body Mass Index
The Body Mass Index (BMI) is a measure that
uses your height and weight to work out if your
weight is healthy.
Your program should take the BMI value as a
float from input, and output one of the following
categories, based in its value:
In case BMI is below 18.5, output
"Underweight".
In case BMI is over 25, output "Overweight'
For all other values, output "Normal"
Make sure to output the correct
category name, exactly as provided
above.
0
The exercise doesn't mention obesity, but the test shows "Overweight" when the BMI result is 20.6.
0
Arin Just delete the space in the first line like this and it will work.
#include <stdio.h>
0
i removed space, but the code didn't work again
0
How are you running the code?
0
looks fine. if you still get issues its due to the compiler i assume. 1 possible case is because you comparing float with double.
The number 18.5 is a double, to make it float need write it like this 18.5f. Do the same for the rest numbers. Always 3nsure that you comparing same types.