forked from dimpeshmalviya/C-Language-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmi_calculator.c
More file actions
32 lines (31 loc) · 888 Bytes
/
bmi_calculator.c
File metadata and controls
32 lines (31 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
bmi_calculator.c
Calculate BMI from weight (kg) and height (m) and print category.
Compile:
gcc -o bmi_calculator bmi_calculator.c
Usage:
./bmi_calculator 70 1.75
Output:
BMI = 22.86 (Normal weight)
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <weight_kg> <height_m>\n", argv[0]);
return 1;
}
double weight = atof(argv[1]);
double height = atof(argv[2]);
if (weight <= 0 || height <= 0) {
fprintf(stderr, "Weight and height must be positive numbers.\n");
return 1;
}
double bmi = weight / (height * height);
printf("BMI = %.2f ", bmi);
if (bmi < 18.5) printf("(Underweight)\n");
else if (bmi < 25.0) printf("(Normal weight)\n");
else if (bmi < 30.0) printf("(Overweight)\n");
else printf("(Obesity)\n");
return 0;
}