{}
run-icon
main.c
#include <stdio.h> int main() { int choice; float height, weight, BMI; char weight_check, height_check; printf("=== Welcome to the BMI Calculator ===\n"); do { // Reset validation flags weight_check = 'n'; height_check = 'n'; // Get valid weight while (weight_check != 'k') { printf("Please enter your weight in kilograms (2.0 - 900.0): "); scanf("%f", &weight); if (weight >= 2.0 && weight <= 900.0) { weight_check = 'k'; printf("Thank you for your input.\n"); } else { printf("Invalid weight. Try again.\n"); } } // Get valid height while (height_check != 'k') { printf("Please enter your height in meters (0.5 - 4.5): "); scanf("%f", &height); if (height >= 0.5 && height <= 4.5) { height_check = 'k'; printf("Thank you for your input.\n"); } else { printf("Invalid height. Try again.\n"); } } // Calculate BMI BMI = weight / (height * height); printf("\nYour BMI is: %.2f\n", BMI); // Determine BMI category if (BMI < 18.5) { printf("You are underweight.\n"); } else if (BMI >= 18.5 && BMI <= 24.9) { printf("You have a normal weight.\n"); } else if (BMI >= 25 && BMI <= 29.9) { printf("You are overweight.\n"); } else { printf("You are obese.\n"); } // Menu for user printf("\nMenu:\n"); printf("1. Calculate BMI again\n"); printf("2. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); } while (choice == 1); printf("Thank you for using the BMI Calculator. Goodbye!\n"); return 0; }
Output