ホームページ  >  記事  >  バックエンド開発  >  指定された年がうるう年かどうかを確認するプログラムを C 言語で作成します。

指定された年がうるう年かどうかを確認するプログラムを C 言語で作成します。

王林
王林転載
2023-09-20 15:33:101413ブラウズ

指定された年がうるう年かどうかを確認するプログラムを C 言語で作成します。

通常の年は 365 日ですが、閏年は 366 日ですが、その年が閏年であるかどうかをプログラムで確認するのが課題です。

判定ロジックは、年が 400 または 4 で割り切れるかどうかで実現できますが、この 2 つの数字で割り切れない場合は、通常の年となります。

Input-: year=2000
Output-: 2000 is a Leap Year

Input-: year=101
Output-: 101 is not a Leap year

アルゴリズム

Start
Step 1 -> declare function bool to check if year if a leap year or not
bool check(int year)
   IF year % 400 = 0 || year%4 = 0
      return true
   End
   Else
      return false
   End
Step 2 -> In main()
   Declare variable as int year = 2000
   Set check(year)? printf("%d is a Leap Year",year): printf("%d is not a Leap Year",year)
   Set year = 10
   Set check(year)? printf("%d is a Leap Year",year): printf("</p><p>%d is not a Leap Year",year);
Stop

#include <stdio.h>
#include <stdbool.h>
//bool to check if year if a leap year or not
bool check(int year){
   // If a year is multiple of 400 or multiple of 4 then it is a leap year
   if (year % 400 == 0 || year%4 == 0)
      return true;
   else
      return false;
}
int main(){
   int year = 2000;
   check(year)? printf("%d is a Leap Year",year): printf("%d is not a Leap Year",year);
   year = 101;
   check(year)? printf("%d is a Leap Year",year): printf("</p><p>%d is not a Leap Year",year);
   return 0;
}

出力

2000 is a Leap Year
101 is not a Leap Year

以上が指定された年がうるう年かどうかを確認するプログラムを C 言語で作成します。の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。