Quantcast
Channel: C - Codecademy Forums
Viewing all articles
Browse latest Browse all 25

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

$
0
0

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.

3 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 25

Latest Images

Trending Articles


Telangana Ration Card Online Status Ahara Bhadratha Card Online Status


Students hit streets to save Agriculture College land in city


Waves Complete v2019.02.14 Incl Emulator-R2R


CalCen


[RELEASE THREAD]--_A-Team_--Cricket_Dream_5G


New! K-12 Display Bulletin Boards for Grade 5 (1st Quarter)


HI-FI SET - PASADENA PARK


Police blotter for Jan. 12


WordPress: Reverse Shell


Artbeats - Nature: Grow! (HD + SD)



Latest Images