卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章21089本站已运行3411

在C语言中编写一个程序,用于检查给定的年份是否为闰年

在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
卓越飞翔博客
上一篇: 学习PHP中鸽巢原理算法的应用场景及实现步骤。
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏