C言語でうるう年であるか平年であるかを判定するには

閏年かどうかを判断するには次のようなルールに従う

  1. 4で割り切れて100で割り切れない年は閏年です。
  2. ただしその年が 400 で割り切れる年はうるう年である。
  3. それ以外の年は平年です。

C言語では、以下のコードを使って年がうるう年かどうかを判断できます。

#include <stdio.h>

int main() {
    int year;
    printf("请输入一个年份:");
    scanf("%d", &year);

    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
        printf("%d年是闰年。", year);
    } else {
        printf("%d年是平年。", year);
    }

    return 0;
}

コードはまずscanf関数でユーザーから年を入力させ、if文でその年が閏年かどうかを判定し、結果を出力します。

bannerAds