Wrong result when calculating BMI

2

I'm doing an exercise with the C language to calculate the BMI, it does not give any error in the compiler but the result goes all wrong, I already looked at the code and did not find the error at all.

The code is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

int main()
{
    setlocale(LC_ALL,"PORTUGUESE");
    printf("Claculo de imc\n");
    //variaveis peso e altura
    double peso;
    double altura;
    printf("Por favor digite seu peso: ");
    scanf("%f",&peso);
    printf("por favor digite sua altura: ");
    scanf("%f",&altura);



  double imc;
  imc = peso/(altura *altura );
  printf("Seu imc é de %.2f",imc);



    system("pause");
    return 0;
}
    
asked by anonymous 01.06.2017 / 01:47

1 answer

4

The problem was that you were using the %f format instead of %lf , which is correct for use with double . Just change to %lf in both printf and scanf .

No printf is optional, however it is recommended.

Code changed:

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

int main()
{
    setlocale(LC_ALL,"PORTUGUESE");
    printf("Claculo de imc\n");
    //variaveis peso e altura
    double peso;
    double altura;
    printf("Por favor digite seu peso: ");
    scanf("%lf",&peso);
    printf("por favor digite sua altura: ");
    scanf("%lf",&altura);

    double imc;
    imc = peso/(altura *altura );
    printf("Seu imc é de %.2lf",imc);

    system("pause");
    return 0;
}
    
01.06.2017 / 03:41