在这个 C 程序中,我们需要根据厘米身高对人进行排序,并将其分类为高、矮和中等。为此,我们需要从用户那里获取身高。使用 `if` 条件语句检查每个用户的身高。如果身高小于 150 厘米,我们将其归类为矮。如果身高在 150 到 165 厘米之间,则称为中等身高。如果身高在 165 到 195 厘米之间,则属于较高的人。
该程序的逻辑首先声明变量“ht”为浮点型。从用户读取厘米身高并将其保存到变量“ht”中。现在检查 if (ht<150),则显示身高为“矮”。使用 else if (ht<150 并且 ht<=165),则显示“中等身高”。否则,检查 else if (ht>=165 并且 ht<=195),则显示“高”。否则显示“异常身高”。
步骤 1: 包含头文件以使用内置头文件。
步骤 2: 声明变量 ht 为浮点型。
步骤 3: 读取用户的身高到 ht。
步骤 4: 检查 if "ht<150" 显示为“矮”。
步骤 5: 使用 else if "ht>=150" 和 "ht<=165" 检查,然后显示“中等身高”。
步骤 6: 使用 else if "ht>=165" 和 "ht<=195" 检查,然后显示“高”,
步骤 7: 否则显示“异常身高”。
为了对人的身高进行分类,我们正在使用下面的 C 编程主题。请参考这些主题以更好地理解
#include <stdio.h>
void main() {
float ht;
printf("Enter the Height (in centimeters)\n");
scanf("%f", & ht);
/* using if we compare the heights */
if (ht < 150)
printf("This person is Short\n");
else if ((ht >= 150) && (ht <= 165.0))
printf("This person is Average Height\n");
else if ((ht >= 165.0) && (ht <= 195.0))
printf("This person is Tall\n");
else
printf("This person is of Abnormal height\n");
} /* End of main() */
Enter the Height (in centimeters) 145 This person is Short