Calculate Body Mass Index in C

6

I'm doing a simple C program to calculate body mass index. However, it is returning a different (wrong) value than a regular 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²

    
asked by anonymous 02.10.2015 / 17:10

2 answers

8

#include<stdio.h>#include<conio.h>intmain(){floatheight,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 must include the #include <conio.h> getch(); is not part of the default language

    
02.10.2015 / 17:28
1

You are doing imc = height / (weight * height); ie height / (weight * height). It should be imc = height / (weight * weight);

    
02.10.2015 / 17:21