Learn-c-by-examples-leap-year-program-in-c
提供:Dev Guides
Cのうるう年プログラム
年を見つけることは飛躍であるかどうかは少し難しいです。 一般に、年数が4で割り切れる場合はうるう年と仮定します。 しかし、それだけではありません。 年はle年です-
- 100で割り切れる
- 100で割り切れる場合、400で割り切れるはずです。
- これを除き、4で割り切れる他の年はすべてうるう年です。
1年が飛躍するかどうかを調べるプログラムを作成する方法を見てみましょう。
アルゴリズム
このプログラムのアルゴリズムは-
START
Step 1 → Take integer variable year
Step 2 → Assign value to the variable
Step 3 → Check if year is divisible by 4 but not 100, DISPLAY "leap year"
Step 4 → Check if year is divisible by 400, DISPLAY "leap year"
Step 5 → Otherwise, DISPLAY "not leap year"
STOP
流れ図
以下に示すように、このプログラムのフロー図を描くことができます-
疑似コード
このアルゴリズムの擬似コードは次のようになります-
procedure leap_year()
IF year%4 = 0 AND year%100 != 0 OR year%400 = 0
PRINT year is leap
ELSE
PRINT year is not leap
END IF
end procedure
実装
このアルゴリズムの実装は以下のとおりです-
#include <stdio.h>
int main() {
int year;
year = 2016;
if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
printf("%d is a leap year", year);
else
printf("%d is not a leap year", year);
return 0;
}
出力
プログラムの出力は次のようになります-
2016 is a leap year