I am wanting to write a program for a BMI calculator that eventually will record details for several entries.

However to start with I need to be able to just complete one calculation.

Below is my code which works up to the if statement what I am trying to do is once I have completed the calculation be able to take that result and use the if statement to indicate if the result is normal, under or over.

I don't know how to take the result and put it into the if statement.

  #include <stdio.h>
#include <math.h>
int main()
{

   float height, weight,bmi;


   printf("Enter Weight in Kg's and Height in METERS  \n");
   scanf("%f%f", &weight, &height );

   bmi = weight/(height*2);


   printf("Your BMI is %.2f\n",bmi);

   scanf("%");

  if("%f"<=25){
                printf("your weight is normal\n");
}


return(0);
{

Which mean your result is equivalent to your bmi variable?
If do so, why don't you use bmi to do it?

On line12 use bmi = weight/(height*height);
2 times height is not the same as height squared. As should be.

On line 19 use if(bmi <= 25) ...

Also, check that height is != 0.0, otherwise you will get a floating point exception and your program will dump core.

I have made some changes as per recommendations, however now I am having a new problem as I cant get the correct result.

// C:\GCC_4_8_1\MinGW\set_distro_paths.bat  needs to be done each time command prompt is open
//gcc -std=c11 -Wall -Wextra -pedantic-errors -O3 -static-libgcc -ocalc.exe calc.c
// to compile a program.
// gcc -std=c11 -Wall -Wextra -pedantic-errors -O3 -static-libgcc -ocalc.exe calc.c 
// to create an executable file.
//*********************************************************************************************//



#include <stdio.h>
#include <math.h>
int main()
{

   float height, weight,bmi;


   printf("Enter Weight in Kg's and Height in METERS  \n");
   scanf("%f%f", &weight, &height );

   bmi = weight/(height*height);


   printf("Your BMI is %.2f\n",bmi);

   if(bmi<=18.5) {
                 printf("you are under weight\n");
}


  if(bmi>=25){
                printf("your weight is normal\n");
}

if(bmi>=50){
            printf("you are over weight\n");
            }


return(0);
}

Unless there are other calculations you don't need to include math.h -- the calculations in your program are all standard C statements.

switch the two statements on lines 31 and 35 -- test if bmi >= 50 before testing if bmi >= 25.

Your program will be a little more efficient if you use if-else statements

if( bmi <= 25)
{

}
else if(bmi >= 50)
{

}
else
{
  // bmi is betwen 25 and 5p0
}

Edited 3 Years Ago by Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.