首頁  >  文章  >  後端開發  >  在C語言中編寫一個程序,用於檢查給定的年份是否為閏年

在C語言中編寫一個程序,用於檢查給定的年份是否為閏年

王林
王林轉載
2023-09-20 15:33:101415瀏覽

在C語言中編寫一個程序,用於檢查給定的年份是否為閏年

閏年有366天,而普通年有365天,任務是透過程式檢查給定的年份是否為閏年。

判斷的邏輯可以透過檢查年份是否能被400或4整除來實現,但如果不能被這兩個數整除,則為普通年。

範例

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

Example

#
#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中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除