C语言程序比较两个数字大小 - 代码示例及解析
下面是针对习题5的C程序代码:
#include <stdio.h>
int main() {
int num1, num2;
printf("请输入两个数字,用空格隔开:");
scanf("%d %d", &num1, &num2);
if (num1 > num2) {
printf("The first number is greater than the second.
");
} else {
printf("The first number is not greater than the second.
");
}
return 0;
}
对于习题5中编写的程序,用户需要输入两个数字作为比较的对象。程序通过scanf()函数调用接收这两个数字,并使用if语句进行比较。如果第一个键入的数字大于第二个数字,则输出消息'The first number is greater than the second';否则,输出消息'The first number is not greater than the second'。
如果输入的两个数字相等,即num1和num2相等,那么程序将输出'The first number is not greater than the second'。因为两个数字相等,没有一个大于另一个。所以即使输入的数字是5和8,或者2和11,只要它们相等,程序都会输出'The first number is not greater than the second'。
原文地址: https://www.cveoy.top/t/topic/pc2 著作权归作者所有。请勿转载和采集!