C/C++ のモジュロ演算子 (%) と例

C または C++ では、 モジュロ演算子 (モジュラス演算子とも呼ばれます) % で示される 算術演算子 。モジュロ除算演算子は次のように生成します。 残り 演算の係数とも呼ばれる整数の除算。

剰余演算子の構文

x と y が整数の場合、式は次のようになります。

x   %   y; 

と発音される x モディファイ y。 たとえば、10 % 2 は Ten mod Two と発音されます。

モジュロ演算子の戻り値

  • y が x を完全に除算する場合、式の結果は 0 になります。
  • x が y で完全に割り切れない場合、結果は範囲 [0, y-1] の余りになります。
  • (バツ % そして) < (x / 2) ………if (x>= y)
  • (バツ % y) = x ……… if (x
  • y が 0 の場合、 ゼロ除算 です コンパイル時エラー

モジュロ演算子の例

以下は、モジュロ演算子の動作を示す C/C++ プログラムです。

C++




// C++ Program to demonstrate the working of modulo operator> #include> using> namespace> std;> // Driver code> int> main(> void> )> {> > int> x, y;> > int> result;> > x = 3;> > y = 4;> > // using modulo operator> > result = x % y;> > cout < < result < < endl;> > result = y % x;> > cout < < result < < endl;> > // for different values> > x = 4;> > y = 2;> > result = x % y;> > cout < < result;> > return> 0;> }> // This code is contributed by Mayank Tyagi>

C




// C Program to illustrate the working of modulo operator> #include> int> main(> void> )> {> > int> x, y;> > int> result;> > x = 3;> > y = 4;> > // using modulo operator> > result = x % y;> > printf> (> '%d'> , result);> > result = y % x;> > printf> (> ' %d'> , result);> > // for different values> > x = 4;> > y = 2;> > result = x % y;> > printf> (> ' %d'> , result);> > return> 0;> }>

モジュロ演算子の制限事項

モジュロ演算子にはほとんど制限がありません。の % モジュラス演算子 には適用できません 浮動小数点数 つまり、float または double です。浮動小数点定数または変数でモジュロ演算子を使用しようとすると、コンパイラによってエラーが生成されます。

例 1: モジュロ演算子の制限を示す C/C++ プログラム。

C++




// C++ Program to demonstrate the restrictions of modulo> // operator> #include> using> namespace> std;> // Driver code> int> main()> {> > float> x, y;> > x = 2.3;> > y = 1.5;> > // modulo for floating point values> > result = x % y;> > cout < < result;> > return> 0;> }> // This code is contributed by Harshit Srivastava>

C




// C Program to illustrate the working of modulo operator> #include> int> main(> void> )> {> > float> x, y;> > float> result;> > x = 2.3;> > y = 1.5;> > // modulo for floating point values> > result = x % y;> > printf> (> '%f'> , result);> > return> 0;> }>

出力

Compilation Error in C code :- prog.c: In function 'main': prog.c:19:16: error:  invalid operands to binary % (have 'float' and 'float')  result = x % y;  ^ 

負のオペランドのモジュロ演算子

アンダーフローまたはオーバーフローの結果としてアクションが実行されるため、負のオペランドのモジュロ演算子の結果の符号はマシンに依存します。

例 2: 負のオペランドのモジュロ演算子を示す C/C++ プログラム。

C++




// C++ Program to demonstrate the working of the modulo> // operator for negative operands> #include> using> namespace> std;> // Driver code> int> main(> void> )> {> > int> x, y;> > int> result;> > x = -3;> > y = 4;> > // modulo for negative operands> > result = x % y;> > cout < < result < < endl;> > x = 4;> > y = -2;> > result = x % y;> > cout < < result < < endl;> > x = -3;> > y = -4;> > result = x % y;> > cout < < result;> > return> 0;> }> // This code is contributed by Harshit Srivastava>

C




// C Program to illustrate the working of the modulo> // operator with negative operands> #include> int> main(> void> )> {> > int> x, y;> > int> result;> > x = -3;> > y = 4;> > // modulo for negative operands> > result = x % y;> > printf> (> '%d'> , result);> > x = 4;> > y = -2;> > result = x % y;> > printf> (> ' %d'> , result);> > x = -3;> > y = -4;> > result = x % y;> > printf> (> ' %d'> , result);> > return> 0;> }>

出力

-3 0 -3 

注記: この場合の戻り値はコンパイラに依存します。

モジュロ演算子に関するよくある質問

Q1.モジュールを定義します。

答え:

C/C++ プログラミング言語では、mod は、ある数値を別の数値で割って余りを返す数学演算を指します。

を使用して実行できます オペレータモジュール (%)

Q2. mod算術とは何ですか?

答え:

Mod 演算とは、数値が常にその特定の点よりも小さくなるように、特定の点の周囲をラップし続けるプロセスを指します。例えば、

数字を考えてみる n = 10 そしてその 点 p = 20。
n を 10 回インクリメントすると、n = 20 になりますが、剰余算術では、指定された点よりもはるかに小さくなるはずです。

そのための 1 つの方法は、次のようにモジュロ演算子を使用することです。

n++; n = n % p; 

モジュラー算術について詳しくは、次の記事を参照してください。 モジュラー算術

Q3.モジュロ演算子と除算演算子の違いは何ですか?

答え:

モジュロ演算子と除算演算子の主な違いは次のとおりです。

  • オペレーターモジュール (%) ある数値を他の数値で除算した後の剰余を返します。
  • 除算演算子 (/) ある数値を他の数値で除算した後の商を返します。