Easy 0 solved 0% acceptance

Body Mass Index

Compute BMI from weight and height.

What you'll learn

  • Square a value and divide
  • Work with decimals
  • Translate a formula into code

Problem

Body mass index (BMI) is weight in kilograms divided by the square of height in meters.

Implement body_mass_index(weight_kg, height_m).

Input

weight_kg: Weight in kilograms. height_m: Height in meters.

Output

Return weight_kg / (height_m × height_m).

Constraints

1 ≤ weight_kg ≤ 500; 0.5 ≤ height_m ≤ 2.5

Examples

Example 1
Input:
height_m = 1.8
weight_kg = 72.0

Expected output:
expected = 22.222222
Input:
double height_m = 1.8;
double weight_kg = 72.0;

Expected output:
double expected = 22.222222;
Input:
double height_m = 1.8;
double weight_kg = 72.0;

Expected output:
double expected = 22.222222;

Explanation

1.8 × 1.8 = 3.24 and 72 / 3.24 ≈ 22.2222.

Example 2
Input:
height_m = 1.0
weight_kg = 50.0

Expected output:
expected = 50.0
Input:
double height_m = 1.0;
double weight_kg = 50.0;

Expected output:
double expected = 50.0;
Input:
double height_m = 1.0;
double weight_kg = 50.0;

Expected output:
double expected = 50.0;

Explanation

With a height of 1 meter the BMI equals the weight: 50.

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

BMI compares weight to height squared, so the height term matters twice.

Hint 2

Square the height first or be careful with parentheses when dividing.

Hint 3

Use decimal division, because the answer is usually not an integer.

Expected complexity

O(1) time, O(1) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.