0
0
PythonProgramBeginner · 2 min read

Python Program to Calculate BMI with Input and Output

You can calculate BMI in Python by taking weight in kilograms and height in meters, then using bmi = weight / (height ** 2) to get the BMI value.
📋

Examples

Inputweight = 70, height = 1.75
OutputBMI is 22.86
Inputweight = 50, height = 1.6
OutputBMI is 19.53
Inputweight = 90, height = 1.8
OutputBMI is 27.78
🧠

How to Think About It

To calculate BMI, first get the person's weight in kilograms and height in meters. Then divide the weight by the square of the height. This gives a number that shows if the person is underweight, normal, or overweight.
📐

Algorithm

1
Get the weight input in kilograms
2
Get the height input in meters
3
Calculate BMI by dividing weight by height squared
4
Print the BMI value rounded to two decimal places
💻

Code

python
weight = float(input('Enter weight in kg: '))
height = float(input('Enter height in meters: '))
bmi = weight / (height ** 2)
print(f'BMI is {bmi:.2f}')
Output
Enter weight in kg: 70 Enter height in meters: 1.75 BMI is 22.86
🔍

Dry Run

Let's trace the example where weight is 70 kg and height is 1.75 meters through the code.

1

Input weight

User enters 70, so weight = 70.0

2

Input height

User enters 1.75, so height = 1.75

3

Calculate BMI

bmi = 70.0 / (1.75 ** 2) = 70.0 / 3.0625 = 22.857142857142858

4

Print result

Output: BMI is 22.86

StepWeightHeightBMI CalculationBMI Result
170.0---
270.01.75--
370.01.7570.0 / (1.75 ** 2)22.857142857142858
470.01.75-22.86
💡

Why This Works

Step 1: Getting inputs

We ask the user for weight and height so we have the numbers needed to calculate BMI.

Step 2: Calculating BMI

BMI is weight divided by height squared, so we use bmi = weight / (height ** 2).

Step 3: Showing the result

We print the BMI rounded to two decimals to make it easy to read.

🔄

Alternative Approaches

Using a function
python
def calculate_bmi(weight, height):
    return weight / (height ** 2)

w = float(input('Weight in kg: '))
h = float(input('Height in m: '))
print(f'BMI is {calculate_bmi(w, h):.2f}')
This approach organizes the calculation in a reusable function, making the code cleaner for bigger programs.
Using imperial units (pounds and inches)
python
weight = float(input('Weight in pounds: '))
height = float(input('Height in inches: '))
bmi = 703 * weight / (height ** 2)
print(f'BMI is {bmi:.2f}')
This uses a different formula for BMI when using pounds and inches, common in the US.

Complexity: O(1) time, O(1) space

Time Complexity

The calculation involves only a few arithmetic operations, so it runs in constant time.

Space Complexity

Only a few variables are used, so the space needed is constant.

Which Approach is Fastest?

All approaches run instantly for single inputs; using a function improves code clarity but does not affect speed.

ApproachTimeSpaceBest For
Direct calculationO(1)O(1)Simple scripts
Function-basedO(1)O(1)Reusable code in larger projects
Imperial units formulaO(1)O(1)Users with pounds/inches units
💡
Always convert height to meters and weight to kilograms before calculating BMI for correct results.
⚠️
Forgetting to square the height or mixing units like centimeters with kilograms without conversion.