うるう年をチェックするPythonプログラム

Python

うるう年は、世紀の年(00で終わる年)を除いて、正確に4で割り切れます。 世紀の年は、400で完全に割り切れる場合にのみうるう年です。たとえば、

2017 is not a leap year
1900 is a not leap year
2012 is a leap year
2000 is a leap year

ソースコード

# Python program to check if year is a leap year or not

year = 2000

# To get year (integer input) from the user
# year = int(input("Enter a year: "))

if (year % 4) == 0:
   if (year % 100) == 0:
       if (year % 400) == 0:
           print("{0} is a leap year".format(year))
       else:
           print("{0} is not a leap year".format(year))
   else:
       print("{0} is a leap year".format(year))
else:
   print("{0} is not a leap year".format(year))

出力

2000 is a leap year

の値を変更できます ソースコードで、このプログラムをテストするために再度実行します。



Hope this helps!

Source link

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です