BMI Calculator outputting 0.00

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)

{
int weight;
int height;
int BMI;

printf("Enter a weight (kg) : ");
scanf_s("%d", &weight);

printf("Enter a height (m) : ");
scanf_s("%d", &height);

BMI = weight / (height * height);

if (BMI < 16)
printf("You are seriously underweight. Your BMI is %.2f\n", BMI);
if (BMI > 16 && BMI < 18)
printf("You are underweight. Your BMI is %.2f\n", BMI);
if (BMI > 18 && BMI < 24)
printf("You are normal weight. Your BMI is %.2f\n", BMI);
if (BMI > 24 && BMI < 29)
printf("You are overweight. Your BMI is %.2f\n", BMI);
if (BMI > 29 && BMI < 35)
printf("You are seriously overweight. Your BMI is %.2f\n", BMI);
if (BMI > 35)
printf("You are gravely overweight. Your BMI is %.2f\n", BMI);


return 0;
}

if someone could check this over and let me know the issue. Thank you so much
Use floating point for height, weight and bmi.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>

int main(void)
{
    double weight = 0 ; // *** double
    double height = 0 ; // *** double

    printf("Enter a weight (kg) : ");
    scanf("%lf", &weight);

    printf("Enter a height (m) : ");
    scanf("%lf", &height);
    
    if( weight > 0 && height > 0 ) // sanity check
    {
        const double bmi = weight / (height * height); // *** double

        if (bmi < 16)
            printf("You are seriously underweight. Your bmi is %.2f\n", bmi);
        else if (bmi < 18)
            printf("You are underweight. Your bmi is %.2f\n", bmi);
        else if (bmi < 24)
            printf("You are normal weight. Your bmi is %.2f\n", bmi);
        else if (bmi < 29)
            printf("You are overweight. Your bmi is %.2f\n", bmi);
        else if (bmi < 35)
            printf("You are seriously overweight. Your bmi is %.2f\n", bmi);
        else
            printf("You are gravely overweight. Your bmi is %.2f\n", bmi);
    }
}
In c/c++, if the two numbers being divided are both int, then the result is int.

So 7 / 2 is 3 and not 3.5 as perhaps expected. But 7 / 2.0 (or 7.0 / 2 or 7.0 / 2.0) is 3.5 as at least one of the numbers is floating point - so the result is floating point.
Topic archived. No new replies allowed.