Learn-c-by-examples-compare-two-integers-in-c

提供:Dev Guides
移動先:案内検索

Cの2つの整数を比較する

2つの整数変数の比較は、簡単に作成できる最も単純なプログラムの1つです。 このプログラムでは、 `+ scanf()+`関数を使用してユーザーから入力を受け取るか、プログラム自体で静的に定義できます。

あなたにとってもシンプルなプログラムになると期待しています。 2つの整数変数を比較しているだけです。 最初にアルゴリズムを見てから、そのフロー図に続いて擬似コードと実装を確認します。

アルゴリズム

最初に、2つの整数を比較するための段階的な手順を見てみましょう。

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

流れ図

以下に示すように、このプログラムのフロー図を描くことができます-

整数比較

疑似コード

このアルゴリズムの擬似コードを見てみましょう-

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