在 C 中比较两个整数
比较两个整数变量是您可以轻松编写的最简单的程序之一。在此程序中,您可以使用 scanf()
函数从用户那里获取输入,也可以在程序本身中静态定义。
我们希望它对您来说也是一个简单的程序。我们只是比较两个整数变量。我们将首先查看算法,然后查看其流程图,然后查看伪代码和实现。
算法
让我们首先看看比较两个整数的分步过程应该是怎样的−
开始 步骤 1 → 取两个整数变量,例如 A 和 B 步骤 2 → 为变量赋值 步骤 3 → 如果 A 大于 B,则比较变量 步骤 4 →如果 true 则打印 A 大于 B 步骤 5 → 如果 false 则打印 A 不大于 B 停止
流程图
我们可以为该程序绘制一个流程图,如下所示 −

伪代码
现在让我们看看该算法的伪代码 −
procedure compare(A, B) IF A is greater than B DISPLAY "A is greater than B" ELSE DISPLAY "A is not greater than B" END IF end procedure
实施
现在,我们将看到程序的实际实施 −
#include <stdio.h> int main() { int a, b; a = 11; b = 99; // to take values from user input uncomment the below lines − // printf("Enter value for A :"); // scanf("%d", &a); // printf("Enter value for B :"); // scanf("%d", &b); if(a > b) printf("a is greater than b"); else printf("a is not greater than b"); return 0; }
输出
此程序的输出应为 −
a is not greater than b