#navi(contents-page-name): No such page: 数値

割り算の余りを求める・%

C言語で余りを求める場合は、%を使用します。
以下にサンプルコードを記します。

c-top.html is not found or not readable.

関連記事

C言語で余りを求めるサンプルコード

以下にintの変数を割り算し、余りを求めるサンプルコードと実行例を記します。

C言語で余りを求める場合は、%を使用します。
余り = 値 % 値

&ref(): File not found: "mod.c" at page "数値/余りを求める・%"; (改行コードLF)

#include <stdio.h>

int main(void)
{
    int a = 10;
    int b = 3;
    int c = 2;

    printf("%d / %d = %d...%d\n", a, b, a / b, a % b);
    printf("%d / %d = %d...%d\n", a, c, a / c, a % c);

    return 0;
}

以下にサンプルコードをコンパイルし、実行した時の結果を記します。

$ gcc mod.c -o mod
$ ./mod 
10 / 3 = 3...1
10 / 2 = 5...0

以上、C言語で割り算の余りを求める方法でした。

c-btm.html is not found or not readable.

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS