Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Exercise 2 - Control Structures

BMI Calculator
Exercise Objectives:

● Use control structures to manage the flow of a program


● To develop good programming habits and practices, such as organizing and maintaining
a readable code, documentation, and debugging.

Problem: Create a C program that will compute the body-mass index of a given height and
weight. After calculating the BMI, the program will also tell under which category (underweight,
normal weight, overweight, or obese) it belongs. At the sample run, the user is navigated into a
menu before entering the height and weight. The menu must loop repeatedly until the exit option
is entered. Make sure to have proper error prompts.

How to calculate your Body Mass Index? According to the US National Heart, Lung, and Blood
Institute. You may calculate your BMI into Metric and Standard.

Metric (Kilograms and Meters) Standard (Pounds, Feet and Inches)

BMI = Weight / (Height * Height) TotalHeight = (Feet * 10) + Inches


BMI = (Pounds / (TotalHeight * TotalHeight)) * 703

You may determine your BMI category using this table:


Underweight <18.5

Normal weight 18.5 - 24.9

Overweight 25-29.9

Obesity BMI of 30 or greater

Your program should create two choices in the menu.


(1) Measure in metric: This will ask for the weight in kilograms and height in centimeters.
(2) Measure in standard: This will ask for the weight in pounds in height and feet.
(3) Exit: Once chosen, the program must ask the user either (Y or N) before terminating.

Sample run:
C $gcc -o exer2 exer2.c
C $./exer2
1 - Measure in metric
2 - Measure in standard
3 - Exit

Please enter your choice: 1 Starts measuring in metric.


Enter weight in kilograms: 80
Enter height in centimeters: 170
Your BMI is: 27.68
BMI Category: Overweight
1 - Measure in metric
2 - Measure in standard
3 - Exit

Please enter your choice: 1


Enter weight in kilograms: 70
Enter height in centimeters: 170
Your BMI is: 24.22
BMI Category: Normal weight
1 - Measure in metric
2 - Measure in standard
3 - Exit

Please enter your choice: 1


Enter weight in kilograms: 50
Enter height in centimeters: 170
Your BMI is: 17.30
BMI Category: Underweight
1 - Measure in metric
2 - Measure in standard
3 - Exit

Please enter your choice: 1


Enter weight in kilograms: 120
Enter height in centimeters: 170
Your BMI is: 41.52
BMI Category: Obesity
1 - Measure in metric
2 - Measure in standard
3 - Exit

Please enter your choice: 2 Starts measuring in standard.


Enter weight in pounds: 70
Enter height in feet: 5
Enter height in inches: 7
Your BMI is: 10.96
BMI Category: Underweight

1 - Measure in metric
2 - Measure in standard
3 - Exit

Please enter your choice: 2


Enter weight in pounds: 176.37
Enter height in feet: 5
Enter height in inches: 7
Your BMI is: 27.62
BMI Category: Overweight

1 - Measure in metric
2 - Measure in standard
3 - Exit

Please enter your choice: 2


Enter weight in pounds: 154.32
Enter height in feet: 5
Enter height in inches: 7
Your BMI is: 24.17
BMI Category: Normal weight
1 - Measure in metric
2 - Measure in standard
3 - Exit

Please enter your choice: 2


Enter weight in pounds: 110.23
Enter height in feet: 5
Enter height in inches: 7
Your BMI is: 17.26
BMI Category: Underweight
1 - Measure in metric
2 - Measure in standard
3 - Exit

Please enter your choice: 2


Enter weight in pounds: 220.46
Enter height in feet: 5
Enter height in inches: 7
Your BMI is: 34.53
BMI Category: Obesity
1 - Measure in metric
2 - Measure in standard
3 - Exit
Please enter your choice: 3
Good bye...

You might also like