Calculate Body Mass Index in C

Asked

Viewed 1,679 times

6

I’m doing a simple C-program to calculate body mass index. However, it is returning a different value (wrong) than a common calculator.

#include <stdio.h>

int main() {
    float height, weight, imc;

    printf("Hello World!\nWhat's your height?");
    scanf("%f", &height);

    printf("What's your weight?");
    scanf("%f", &weight);

    imc = height / (weight * height);
    printf("%f", imc);

    getch();
    return 0;
}

Expression: Height / weight²

  • What values are returning? Give an example, please

  • 3

    If it’s weight, you gotta put imc = height / (weight * weight );

  • The result is relative because it depends on the height and weight inserted. But if I put 1.80 and 80 the correct result would be 24.69 but returns 0.555556

  • 2

    BMI is weight (mass) divided by height squared, and not the other way around. And in your code, you’re dividing height by height times weight, which simply gives the inverse of the weight...

2 answers

8


inserir a descrição da imagem aqui

    #include <stdio.h>
    #include <conio.h>

    int main() {
    float height, weight, imc;

    printf("Hello World!\nWhat's your height?");
    scanf("%f", &height);

    printf("What's your weight?");
    scanf("%f", &weight);

    imc = weight / (height * height);
    printf("%f", imc);

    getch();
    return 0;


}

If you want to put the getch() function, you have to include the library #include <conio.h> getch(); is not part of the standard language

1

You’re making imc = height / (weight * height); i.e., height / (weight * height). It should be imc = height / (weight * weight);

  • The formula is wrong

  • 4

    In my opinion, this answer does not merit a negative vote, because it has responded satisfactorily according to the specification. The fact that the specification is wrong does not take the credit, because it is not an obligation of anyone [other than the health area] to know what a BMI is or how to calculate it.

  • 1

    But the question was precisely why the result was wrong, and the answer is that the wording is incorrect

  • @Amadeuantunes The body of the question (except for comments) only says that the result is different from that calculated by a common calculator, and does not give examples, only the formula. The code of this response is consistent with the formula, so the result would not be "wrong". Anyway, I agree with you that the answer is not technically correct (so I didn’t give +1) although I don’t consider it at all wrong (so I didn’t give -1).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.