cplusplus
cplusplus

 

Introduction:

Body Mass Index (BMI) is a measurement that helps assess whether an individual has a healthy body weight in relation to their height. It is commonly used to classify individuals into categories such as underweight, normal weight, overweight, and obese. BMI is calculated using the formula:

        BMI = weight (kg) / height^2 (m^2)

In this program, we will compute the BMI based on the user’s input for their weight and height. The program will then categorize the result and provide feedback about the user’s weight status.

Objective:

The objective of this program is to:

  • Accept user input for weight and height.
  • Calculate the BMI using the provided formula.
  • Classify the BMI value into one of the following categories: Underweight, Normal weight, Overweight, and Obese.
  • Display the BMI result and the corresponding category to the user.

Program Code:

#include 
#include   // for setting precision of output
using namespace std;

int main() {
    double weight, height, bmi;

    // Ask for user input
    cout << "Enter your weight in kilograms (kg): "; cin >> weight;

    cout << "Enter your height in meters (m): "; cin >> height;

    // Calculate BMI
    bmi = weight / (height * height);

    // Display BMI result with 2 decimal places
    cout << fixed << setprecision(2);
    cout << "Your BMI is: " << bmi << endl;

    // Determine and display BMI category
    if (bmi < 18.5) {
        cout << "You are underweight." << endl; } else if (bmi >= 18.5 && bmi < 24.9) {
        cout << "You have a normal weight." << endl; } else if (bmi >= 25 && bmi < 29.9) {
        cout << "You are overweight." << endl;
    } else {
        cout << "You are obese." << endl;
    }

    return 0;
}

Explanation of Program Structure:

This program consists of the following steps:

  1. Input: The program prompts the user to input their weight (in kilograms) and height (in meters).
  2. Calculation: Using the inputted values, the program calculates the BMI using the formula BMI = weight / (height^2).
  3. Output: The program outputs the BMI with two decimal points, followed by a classification based on the calculated BMI value. The categories are:
    • Underweight: BMI < 18.5
    • Normal weight: 18.5 <= BMI < 24.9
    • Overweight: 25 <= BMI < 29.9
    • Obese: BMI >= 30

How to Run the Program:

To run the BMI calculator program, follow these steps:

  1. Write the program code in a C++ editor or IDE (such as Code::Blocks, Dev-C++, or Visual Studio).
  2. Save the program with a “.cpp” extension (e.g., bmi_calculator.cpp).
  3. Compile the program. Most IDEs provide a “Build” or “Compile” option. If you are using the command line, you can use the following command:
                        g++ bmi_calculator.cpp -o bmi_calculator
    
  4. Run the compiled program using the following command:
                        ./bmi_calculator
    
  5. Enter your weight and height as prompted, and the program will calculate and display your BMI along with the classification.
Copyright © Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)