Calculate Your Body Mass Index (BMI) Online with Code

Easily determine your BMI online using this coding-based BMI calculator. Monitor your health and fitness with precision.
Easily determine your BMI online using this coding-based BMI calculator. Monitor your health and fitness with precision.

To create a C# program that calculates and categorizes BMI (Body Mass Index) into different ranges like “underweight,” “normal weight,” “overweight,” etc., you can follow the steps below. First, you’ll need to define the BMI ranges and their corresponding categories, then create a function to calculate the BMI based on user input.

Here’s a sample C# program to get you started:

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("BMI Calculator");
        Console.Write("Enter your weight in kilograms: ");
        double weight = Convert.ToDouble(Console.ReadLine());

        Console.Write("Enter your height in meters: ");
        double height = Convert.ToDouble(Console.ReadLine());

        double bmi = CalculateBMI(weight, height);
        string bmiCategory = GetBMICategory(bmi);

        Console.WriteLine($"Your BMI is {bmi:F2}");
        Console.WriteLine($"Category: {bmiCategory}");
    }

    static double CalculateBMI(double weight, double height)
    {
        return weight / (height * height);
    }

    static string GetBMICategory(double bmi)
    {
        if (bmi < 18.5)
            return "Underweight";
        else if (bmi < 24.9)
            return "Normal weight";
        else if (bmi < 29.9)
            return "Overweight";
        else if (bmi < 34.9)
            return "Obesity (Class 1)";
        else if (bmi < 39.9)
            return "Obesity (Class 2)";
        else
            return "Obesity (Class 3)";
    }
}

In this program:

  1. We prompt the user to enter their weight in kilograms and height in meters.
  2. We calculate the BMI using the CalculateBMI function, which takes weight and height as input and returns the BMI.
  3. We categorize the BMI using the GetBMICategory function, which takes the BMI value as input and returns the corresponding category.
  4. We display the calculated BMI and its category to the user.

You can further customize the BMI categories and their corresponding ranges as per your requirements.

Similar Posts