Introduction

Body Mass Index (BMI) is a measurement that helps assess whether a person has a healthy body weight for a given height.
It is calculated using a simple formula: BMI = weight (kg) / height² (m²). A BMI score falls into different categories
that indicate whether a person is underweight, normal weight, overweight, or obese. This program will help you calculate
your BMI using the formula and classify your weight status based on your BMI value.

Objective

The objective of this program is to create a simple BMI calculator in C. It will allow the user to input their weight
and height, then calculate their BMI and classify their body weight according to established categories.

Program Code

#include 

int main() {
    // Declare variables for weight, height, and BMI
    float weight, height, bmi;

    // Prompt the user to enter their weight in kilograms
    printf("Enter weight in kilograms: ");
    scanf("%f", &weight);

    // Prompt the user to enter their height in meters
    printf("Enter height in meters: ");
    scanf("%f", &height);

    // Calculate BMI using the formula: BMI = weight / (height * height)
    bmi = weight / (height * height);

    // Display the calculated BMI
    printf("Your BMI is: %.2f\n", bmi);

    // Classify BMI and display the appropriate message
    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");
    }

    return 0;
}

Program Explanation

The program works as follows:

  • The program starts by declaring three variables: weight, height, and bmi.
  • The user is asked to input their weight in kilograms and height in meters.
  • The BMI is calculated using the formula: bmi = weight / (height * height).
  • The program then prints the calculated BMI value with two decimal places using printf.
  • Based on the calculated BMI, the program classifies the body weight into one of four categories:
    • Underweight if BMI is less than 18.5.
    • Normal weight if BMI is between 18.5 and 24.9.
    • Overweight if BMI is between 25 and 29.9.
    • Obese if BMI is 30 or higher.
  • Finally, the classification message is displayed to the user.

How to Run the Program

To run the program, follow these steps:

  1. Copy the provided C code into a text file and save it with a .c extension (e.g., bmi_calculator.c).
  2. Open a terminal or command prompt and navigate to the directory where the .c file is saved.
  3. Compile the program using a C compiler, for example using GCC:
    gcc bmi_calculator.c -o bmi_calculator
  4. Run the compiled program:
    ./bmi_calculator
  5. Follow the on-screen prompts to enter your weight and height, and the program will display your BMI and classification.
© 2024 Learn Programming

 

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 :)