要在C语言中计算人的BMI(Body Mass Index)值,可以使用以下公式:
#include <stdio.h>
// 函数:计算BMI值
float calculateBMI(float weight, float height) {
// 将身高从厘米转换为米
height = height / 100.0;
// 计算BMI值
float bmi = weight / (height * height);
return bmi;
}
int main() {
float weight, height;
// 提示用户输入体重(公斤)
printf("请输入体重(公斤):");
scanf("%f", &weight);
// 提示用户输入身高(厘米)
printf("请输入身高(厘米):");
scanf("%f", &height);
// 调用calculateBMI函数计算BMI值
float bmi = calculateBMI(weight, height);
// 打印计算结果
printf("您的BMI值为:%.2f\n", bmi);
return 0;
}
这段代码中,calculateBMI
函数接受体重(以公斤为单位)和身高(以厘米为单位)作为参数,将身高从厘米转换为米,并使用BMI公式计算BMI值。然后,main
函数中通过用户输入获取体重和身高的值,调用calculateBMI
函数计算BMI,并将结果打印出来。
© 版权声明
THE END
暂无评论内容