用 C 比较两个整数



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

这对我们来说也是一个简单的程序。我们只是比较两个整数变量。我们将首先了解算法,然后是流程图,随后是伪代码和实现。

算法

让我们首先看看比较两个整数的分步过程−

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 true print A is greater than B
   Step 5 → If false print A is not greater than B
STOP

流程图

我们可为该程序绘制一个流程图,如下所示−

integer comparison

伪代码

现在我们来看看此算法的伪代码−

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
simple_programs_in_c.htm
广告
© . All rights reserved.