目录

比较两个整数(Compare two integers)

比较两个整数变量是您可以轻松编写的最简单的程序之一。 在此程序中,您可以使用scanf()函数从用户获取输入,也可以在程序本身中静态定义。

我们希望它也是一个简单的程序。 我们只是比较两个整数变量。 我们首先看一下算法,然后是流程图,然后是伪代码和实现。

算法 (Algorithm)

让我们首先看看比较两个整数的分步过程应该是什么 -

START
   Step 1 → Take two integer variables, say A & B
   Step 2 → Assign values to variables
   Step 3 → Compare variables if A is greater than B
   Step 4 → If <u>true</u> print <i>A is greater than B</i>
   Step 5 → If <u>false</u> print <i>A is not greater than B</i>
STOP

流程图 (Flow Diagram)

我们可以绘制这个程序的流程图,如下所示 -

整数比较

伪代码 (Pseudocode)

现在让我们看看这个算法的伪代码 -

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

实现 (Implementation)

现在,我们将看到该计划的实际执行情况 -

#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;
}

输出 (Output)

该计划的输出应为 -

a is not greater than b
↑回到顶部↑
WIKI教程 @2018