C語言入門 -- 計算BMI (2021/1/7)
阿新 • • 發佈:2021-01-08
計算BMI
使用者輸入身高體重,計算BMI
BMI(體重指數)計算公式為:
建立一個BMI計算器應用程式,讀取使用者以磅為單位的體重和以英寸為單位的身高(或者,如果您願意,可以讀取使用者的體重以千克為單位,身高以米為單位),然後計算並顯示使用者的體重指數。此外,應用程式還應顯示來自衛生和公共服務部/國家衛生研究院的以下資訊,以便使用者可以評估其BMI:
顯示資訊 | BMI值 |
---|---|
體重不足 | 小於18.5 |
正常 | 在18.5到24.9之間 |
超重 | 在25到29.9之間 |
肥胖的 | 30或以上 |
/*
Name:programme3.c
Author:祁麟
Copyright:BJTU | school of software
Date:2020/10/11
Description:calculates and displays the user's body mass index.
*/
#include <stdio.h>
int main (){
int unit;
float height,weight,BMI;
printf ("If you choose to calculate in inches and pounds, enter 0\r\n");
printf ("If you choose to calculate in meters and kilograms, enter 1\r\n");
scanf ("%d",&unit);
if (unit==0)
{
printf ("please enter the height and the weight\r\n");
scanf ("%f %f",&height,&weight);
BMI=weight*703/height/height;
printf ("BMI=%f\r\n",BMI);
if (BMI<18.5) {
printf ("Underweight");
}
else if (BMI<24.95 && BMI>=18.5){
printf ("Normal");
}
else if (BMI<29.95 && BMI>=24.95){
printf ("Overweight");
}
else {
printf ("Obese");}
}
else {
printf ("please enter the height and the weight\r\n");
scanf ("%f %f",&height,&weight);
BMI=weight/height/height;
printf ("BMI=%f\r\n",BMI);
if (BMI<18.5){
printf ("Underweight");
}
else if (BMI<24.95 && BMI>=18.5){
printf ("Normal");
}
else if (BMI<29.95 && BMI>=24.95){
printf ("Overweight");
}
else {
printf ("Obese");
}
}
return 0;
}