Software Development Exam  >  Software Development Notes  >  Basics of C++  >  BMI Calculator in C++

BMI Calculator in C++ | Basics of C++ - Software Development PDF Download

Introduction

A Body Mass Index (BMI) calculator helps determine whether a person is underweight, normal weight, overweight, or obese based on their height and weight. In this project, we will create a simple BMI calculator using C++. The program will take user input, calculate BMI, and categorize the result.

Understanding BMI Calculation

The formula for calculating BMI is:
However, if the weight is provided in pounds (lbs) and height in inches (in), we use:

Project Breakdown

This project involves:

  • Taking user input for name, weight (lbs), and height (inches).
  • Calculating BMI using the formula.
  • Categorizing the BMI result.
  • Displaying the output in a user-friendly format.

Code for the BMI Calculator in C++

#include <iostream>
#include <string>
using namespace std;
int main() {
    // Variables for user input
    string name;
    double weight, height, BMI;

    // Taking user input
    cout << "Enter your name: ";
    getline(cin, name);
    cout << "Enter your weight in pounds: ";
    cin >> weight;
    cout << "Enter your height in inches: ";
    cin >> height;

    // BMI Calculation
    BMI = (weight * 703) / (height * height);

    // Displaying the BMI value
    cout << "\n" << name << ", your BMI is: " << BMI << endl;

    // Categorizing BMI
    if (BMI > 0) {
        if (BMI < 18.5)
            cout << "You are underweight." << endl;
        else if (BMI <= 24.9)
            cout << "You have a normal weight." << endl;
        else if (BMI < 29.9)
           cout << "You are overweight. Consider a healthy diet and exercise." << endl;
        else if (BMI < 34.9)
            cout << "You are obese." << endl;

        else if (BMI < 39.9)
            cout << "You are severely obese." << endl;
        else
            cout << "You are morbidly obese." << endl;
    } else {
        cout << "Please enter valid inputs." << endl;
    }
    return 0;
}

Understanding the Code

1. Importing Libraries

#include <iostream>
#include <string>
iostream: Enables input and output operations.
string: Allows us to use string variables for handling names.

2. Declaring Variables and Taking Input

string name;
double weight, height, BMI;
name: Stores the user's name.
weight: Stores the user's weight in pounds.
height: Stores the user's height in inches.
BMI: Stores the calculated BMI.
User input is taken using:
cout << "Enter your name: ";
getline(cin, name);
cout << "Enter your weight in pounds: ";
cin >> weight;
cout << "Enter your height in inches: ";
cin >> height;
getline(cin, name): Reads the full name (including spaces).
cin >> weight; cin >> height;: Reads numerical inputs.

3. Calculating BMI
BMI = (weight * 703) / (height * height);
This formula converts weight and height into BMI.

4. Displaying BMI and Categorizing the Result
if (BMI > 0) {
    if (BMI < 18.5)
        cout << "You are underweight." << endl;
    else if (BMI <= 24.9)
        cout << "You have a normal weight." << endl;
    else if (BMI < 29.9)
        cout << "You are overweight. Consider a healthy diet and exercise." << endl;
    else if (BMI < 34.9)
        cout << "You are obese." << endl;
    else if (BMI < 39.9)
        cout << "You are severely obese." << endl;
    else
        cout << "You are morbidly obese." << endl;
} else {
    cout << "Please enter valid inputs." << endl;
}

  • This checks the BMI value and categorizes the user's weight status accordingly.
  • The program ensures the input values are valid.

How to Run the BMI Calculator

1. Open a C++ Compiler

  • Use GCC, Visual Studio Code, or an online C++ compiler.

2. Copy and Paste the Code

3. Compile and Run the Program

If using a command-line compiler, type:
g++ bmi_calculator.cpp -o bmi_calculator

  • ./bmi_calculator

4. Enter Your Details
Provide your name, weight (lbs), and height (inches).

5. Get Your BMI Result
The program will display your BMI and weight category.

Possible Improvements

  • Allow user to input weight in kilograms and height in meters.
  • Add a feature to calculate Ideal Body Weight (IBW).
  • Improve user interaction with a graphical interface.
  • Allow multiple calculations in a loop.

Conclusion

This simple BMI calculator demonstrates how to take user input, perform calculations, and display meaningful results in C++. It is an excellent beginner-friendly project for learning basic I/O operations, conditional statements, and arithmetic calculations.

The document BMI Calculator in C++ | Basics of C++ - Software Development is a part of the Software Development Course Basics of C++.
All you need of Software Development at this link: Software Development
71 videos|48 docs|15 tests

FAQs on BMI Calculator in C++ - Basics of C++ - Software Development

1. What is a BMI Calculator and how does it work?
Ans. A BMI (Body Mass Index) Calculator is a tool used to assess an individual's body weight in relation to their height. It works by taking the person's weight in kilograms and dividing it by the square of their height in meters (BMI = weight(kg) / height(m)^2). The resulting value helps categorize individuals into various weight categories such as underweight, normal weight, overweight, and obese.
2. What are the benefits of using a BMI Calculator?
Ans. The benefits of using a BMI Calculator include providing a quick and easy assessment of body weight relative to height, helping individuals understand their weight status, and serving as a useful tool for health professionals in evaluating health risks associated with being underweight or overweight. It also encourages individuals to maintain a healthy lifestyle by highlighting the importance of weight management.
3. How do I implement a BMI Calculator in C++?
Ans. To implement a BMI Calculator in C++, you will need to write a program that prompts the user for their weight and height, performs the BMI calculation, and then displays the result. The basic structure involves using input/output functions, arithmetic operations for the calculation, and conditional statements to categorize the BMI result.
4. Can I run the BMI Calculator code on any C++ compiler?
Ans. Yes, you can run the BMI Calculator code on any standard C++ compiler that supports the C++ language, such as GCC, Clang, or Microsoft Visual Studio. Make sure that the compiler is set up correctly and that the code is free from syntax errors to ensure it runs smoothly.
5. What are some common errors to watch out for when coding a BMI Calculator in C++?
Ans. Common errors to watch out for include incorrect data types for input (e.g., using integers instead of floats for weight or height), division by zero (if height input is incorrectly set to zero), and improper formatting of output. Additionally, ensure that the logical conditions for BMI categories are correctly implemented to avoid misclassification.
Related Searches

BMI Calculator in C++ | Basics of C++ - Software Development

,

video lectures

,

Free

,

Summary

,

past year papers

,

ppt

,

Extra Questions

,

Viva Questions

,

MCQs

,

BMI Calculator in C++ | Basics of C++ - Software Development

,

study material

,

Sample Paper

,

Objective type Questions

,

Exam

,

pdf

,

mock tests for examination

,

Important questions

,

practice quizzes

,

BMI Calculator in C++ | Basics of C++ - Software Development

,

Semester Notes

,

shortcuts and tricks

,

Previous Year Questions with Solutions

;