Body Mass Index(BMI) Calculator: Why is my program not working as expected?

Hello! I’ve been trying to program a Body Mass Index(BMI) calculator. In case you don’t know what BMI is, its basically the weight of a person in kilograms divided by height in meters squared. When I was running my program, the output I was getting was not expected.

My original code for BMI calculator:

#include <stdio.h>
#include <ctype.h>

int main() {
	double Weight, Height, BMI, Dummy;
	char Response;
	
	printf("Hello patient! Please enter 'Y' to continue or 'N' exit: ");
	scanf("%c", &Response);
	if (Response == 'y') {
		do {
			/*
			 * Asking the user for their weight.
			 */
			printf("Please enter your weight in kilograms: ");
			scanf(" %f", &Weight);
			/*
			 * Asking the user for their height.
			 */
			printf("Now, please enter your height in meters: ");
			scanf(" %f", &Height);
			/*
			 * Body Mass Index (BMI) is a person's weight in kilograms divided by the square of height in meter.
			 * Also Calculate Body Mass Index(BMI).
			 * Then print BMI.
			 */
			BMI = Weight / (Height * Height);
			printf("Your Body Mass Index (BMI) is %2.1f.\n", BMI);
			/*
			 * Now we will inform the user whether their BMI is good or not.
			 * If your BMI is less than 18.5, it falls within the underweight range.
			 * If your BMI is 18.5 to 24.9, it falls within the Healthy Weight range. 
			 * If your BMI is 25.0 to 29.9, it falls within the overweight range.
			 */
			if (BMI < 18.5) {
				printf("You're BMI falls within Underweight Range.\n");
			} else if (BMI >= 18.5 && BMI <= 24.9) {
				printf("You're BMI falls within Healthy Weight Range.\n");
			} else if (BMI >= 25.0 && BMI <= 29.9) {
				printf("You're BMI falls within Overweight Range./n");
			}
			/* 
			 * Ask the user if wants to check BMI again.
			 */
			
			printf("Do you want to check your Body Mass Index (BMI) again? ");
			scanf(" %c", &Response);
			Response = toupper(Response);
		} while (Response == 'Y');
	}
}

Later, I coded a more simpler program of the BMI calculator, in case there was something wrong with my code of the above program. But it was also showing a more different unexpected result than the above. The simpler code is as below:

#include <stdio.h>

int main() {
	double weight, height, heightSq, bmi;
	
	// For weight.
	printf("Your weight: ");
	scanf(" %f", &weight);
	// For height.
	printf("Your height: ");
	scanf(" %f", &height);
	// Calculating BMI.
	bmi = weight / (height * height);
	printf("Your BMI is %2.1f.\n");
	
	return 0;
}

I can’t understand what’s wrong here. Please tell me what is wrong with my program.

Have a look at:

With the above in mind,

// You wrote:
scanf(" %f", &Weight);

// Change to:
scanf(" %lf", &Weight);

In the simpler version, again the "%lf" format specifier needs to be used in the scanf statements.
Additionally, your final printf statement doesn’t have bmi in it i.e.

// You wrote:
printf("Your BMI is %2.1f.\n");

// Change to:
printf("Your BMI is %2.1f.\n", bmi);
2 Likes

Thanks for the simple explanation. It worked!

1 Like