Below is a C# BMI and BMR Calculator program.
The program calculates the Body Mass Index (BMI) and Basal Metabolic Rate (BMR) based on user input for weight, height, age, and gender.
Full Code
using System; class BMICalculator { static void Main(string[] args) { Console.WriteLine("Welcome to the BMI and BMR Calculator!"); // Collect user inputs Console.Write("Enter your weight in kilograms: "); double weight = GetValidInput(); Console.Write("Enter your height in meters: "); double height = GetValidInput(); Console.Write("Enter your age in years: "); int age = (int)GetValidInput(); Console.Write("Enter your gender (Male/Female): "); string gender = Console.ReadLine().Trim().ToLower(); // Calculate BMI double bmi = CalculateBMI(weight, height); Console.WriteLine($"\nYour BMI is: {bmi:F2}"); Console.WriteLine($"BMI Category: {BMICategory(bmi)}"); // Calculate BMR double bmr = CalculateBMR(weight, height, age, gender); if (bmr > 0) { Console.WriteLine($"Your BMR is: {bmr:F2} calories/day"); } else { Console.WriteLine("Invalid gender entered. Unable to calculate BMR."); } } static double GetValidInput() { double value; while (true) { if (double.TryParse(Console.ReadLine(), out value) && value > 0) { return value; } else { Console.Write("Invalid input. Please enter a positive numeric value: "); } } } static double CalculateBMI(double weight, double height) { return weight / (height * height); } static string BMICategory(double bmi) { if (bmi < 18.5) return "Underweight"; if (bmi >= 18.5 && bmi < 24.9) return "Normal weight"; if (bmi >= 25 && bmi < 29.9) return "Overweight"; return "Obesity"; } static double CalculateBMR(double weight, double height, int age, string gender) { if (gender == "male") { // Harris-Benedict formula for men return 10 * weight + 6.25 * (height * 100) - 5 * age + 5; } else if (gender == "female") { // Harris-Benedict formula for women return 10 * weight + 6.25 * (height * 100) - 5 * age - 161; } else { // Invalid gender return -1; } } }
Explanation
1. Introduction
- The program starts by greeting the user and providing a simple interface for inputting data such as weight, height, age, and gender.
2. User Input Validation
- The GetValidInput method ensures all numeric inputs (weight, height, and age) are positive numbers.
- If the user enters invalid data, the program prompts them to try again until a valid input is provided.
static double GetValidInput() { double value; while (true) { if (double.TryParse(Console.ReadLine(), out value) && value > 0) { return value; } else { Console.Write("Invalid input. Please enter a positive numeric value: "); } } }
3. BMI Calculation
- The Body Mass Index (BMI) is calculated using the formula: BMI=weightheight2BMI = \frac{\text{weight}}{\text{height}^2}
- The result is formatted to two decimal places and categorized based on standard BMI categories.
static double CalculateBMI(double weight, double height) { return weight / (height * height); } static string BMICategory(double bmi) { if (bmi < 18.5) return "Underweight"; if (bmi >= 18.5 && bmi < 24.9) return "Normal weight"; if (bmi >= 25 && bmi < 29.9) return "Overweight"; return "Obesity"; }
Example:
- Weight: 70 kg
- Height: 1.75 m
- BMI = 701.752=22.86\frac{70}{1.75^2} = 22.86 (Normal weight)
4. BMR Calculation
- The Basal Metabolic Rate (BMR) is calculated using the Harris-Benedict formula:
- For Males: BMR=10⋅weight+6.25⋅height (cm)−5⋅age+5BMR = 10 \cdot \text{weight} + 6.25 \cdot \text{height (cm)} – 5 \cdot \text{age} + 5
- For Females: BMR=10⋅weight+6.25⋅height (cm)−5⋅age−161BMR = 10 \cdot \text{weight} + 6.25 \cdot \text{height (cm)} – 5 \cdot \text{age} – 161
- The program validates gender input and calculates BMR accordingly. If the gender is invalid, it returns -1.
static double CalculateBMR(double weight, double height, int age, string gender) { if (gender == "male") { return 10 * weight + 6.25 * (height * 100) - 5 * age + 5; } else if (gender == "female") { return 10 * weight + 6.25 * (height * 100) - 5 * age - 161; } else { return -1; } }
Example:
- Weight: 70 kg
- Height: 1.75 m (converted to 175 cm)
- Age: 25 years
- Gender: Male
- BMR = 10⋅70+6.25⋅175−5⋅25+5=1653.75 calories/day10 \cdot 70 + 6.25 \cdot 175 – 5 \cdot 25 + 5 = 1653.75 \, \text{calories/day}
5. Output
- The program displays:
- BMI with its category (e.g., Normal weight, Overweight).
- BMR with a message indicating daily calorie requirements.
Console.WriteLine($"\nYour BMI is: {bmi:F2}"); Console.WriteLine($"BMI Category: {BMICategory(bmi)}"); Console.WriteLine($"Your BMR is: {bmr:F2} calories/day");
Sample Run
Example Run:
Welcome to the BMI and BMR Calculator! Enter your weight in kilograms: 70 Enter your height in meters: 1.75 Enter your age in years: 25 Enter your gender (Male/Female): Male Your BMI is: 22.86 BMI Category: Normal weight Your BMR is: 1653.75 calories/day
Invalid Input Example:
Enter your weight in kilograms: -50 Invalid input. Please enter a positive numeric value: 70
Gender Validation Example:
Enter your gender (Male/Female): Unknown Invalid gender entered. Unable to calculate BMR.
Features
- Input Validation:
- Ensures weight, height, and age are positive numbers.
- Handles invalid or unexpected gender inputs.
- BMI Calculation:
- Calculates BMI using a standard formula and categorizes it based on health guidelines.
- BMR Calculation:
- Calculates daily calorie requirements based on the Harris-Benedict formula, considering gender, weight, height, and age.
- Reusable Methods:
- Modular design with separate methods for BMI and BMR calculations, input validation, and BMI categorization.
Ideas for Enhancement
- Activity Factor:
- Include activity levels (e.g., sedentary, active) to estimate total daily energy expenditure (TDEE).
Example:
Console.Write("Enter your activity level (Sedentary, Active, Very Active): ");
- Unit Conversion:
- Allow users to input height in feet/inches or weight in pounds, then convert to metric.
- Save Results:
- Save BMI and BMR results to a file for future reference.
- Graphical User Interface:
- Use Windows Forms or WPF to create a user-friendly interface.
This C# BMI and BMR Calculator provides a simple yet effective way to calculate key health metrics