LearnFrenzy
Sign in Take a free test

Operators and Expressions · Operator Programs · Question 7

BMI calculator with categories in C Solution, output & explanation

  • Program
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

Compute the body mass index (weight / height²) and its category for a weight of 68 kg and a height of 1.72 m.

Solution
main.c
#include <stdio.h>

int main(void) {
    double weight = 68, height = 1.72;
    double bmi = weight / (height * height);
    const char *category = bmi < 18.5 ? "underweight" : bmi < 25 ? "normal" : bmi < 30 ? "overweight" : "obese";
    printf("BMI %.2f (%s)\n", bmi, category);
    return 0;
}

Output Verified by compiling and running

BMI 22.99 (normal)

The brackets around height * height are essential: weight / height * height would divide and then multiply back, giving 68. The chained conditional picks the first matching range.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links