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; }

7th May 2024, 3:13 PM
Arin
Arin - avatar
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
10th May 2024, 12:23 PM
Diana🌷
Diana🌷 - avatar
+ 2
The "Obesity" range is missing. Also, the "Overweight" range should include BMI==25.
7th May 2024, 3:19 PM
Brian
Brian - avatar
+ 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.
7th May 2024, 3:59 PM
Brian
Brian - avatar
+ 1
Arin did you make the input correction that I pointed out (to read bmi instead of weight and height)?
10th May 2024, 12:01 PM
Brian
Brian - avatar
+ 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.
10th May 2024, 1:50 PM
Brian
Brian - avatar
0
The exercise doesn't mention obesity, but the test shows "Overweight" when the BMI result is 20.6.
7th May 2024, 3:26 PM
Arin
Arin - avatar
0
Arin Just delete the space in the first line like this and it will work. #include <stdio.h>
10th May 2024, 8:39 AM
Diana🌷
Diana🌷 - avatar
0
i removed space, but the code didn't work again
10th May 2024, 10:00 AM
Arin
Arin - avatar
0
How are you running the code?
10th May 2024, 10:15 AM
Diana🌷
Diana🌷 - avatar
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.
18th May 2024, 10:29 PM
john ds
john ds - avatar