- #1
Marcin H
- 306
- 6
Homework Statement
Write a few lines of code that will accomplish the following:
- Ask the user to first enter his/her weight (in kilograms) and second enter the height (in meters). For every field to be filled in, print on screen the messages "Type your weight: " and "Type your height: " respectively.
- Print on screen on a different line the message "Your BMI is: " and the BMI value of the user according to the following formula:
Note: for this problem, there is no need to write any #include preprocessor directives in the solution
Hint: the formula is easy to implement if you remember the definition of power of a number
Homework Equations
C language
The Attempt at a Solution
I tried running this program and everything works fine except the last step where it should do the calculation and output the BMI. I am not sure what I did wrong. This is my first time programming with C, so I am sorry if this is a dumb/easy mistake, but I am not sure what I am missing. Why is the program not outputting an answer? Using an online compiler I get this as a result:
sh-4.3$ main
Type your weight in kilograms:87
Type your height in meters:1.4
Your BMI is 44sh-4.3$
Here is my code:
Code:
#include <stdio.h>
int main()
{
float weight, height, BMI;
printf("Type your weight in kilograms:");
scanf("%f", &weight);
printf("Type your height in meters:");
scanf("%f", &height);
BMI = weight / (height*height);
printf("Your BMI is %.f",BMI);
return 0;
}