栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c语言判断某个年份是否为闰年

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c语言判断某个年份是否为闰年

分析:

1.年份可以被4整除,但是不可以被100整除。

2.年份可以被400整除。

本次自己搭建函数,对条件进行分析,使用if语句对年份筛选,分别以n求余4等于0,n求余100不能等于0,n求余400等于0作为分别闰年与非闰年的条件。

即if((n%4==0) && (n%100!=0) || (n%400==0)

本文使用了两种自己构造函数的方式

1.有返回值的自定义函数

#include "stdio.h"
int year(int n)
{
	if((n%4==0 && n%100!=0) ||n%400==0)
		return(1);
	else
		return(0);
}
int main()
{
	int a,b;
	printf("Please input a year to check itn");
	scanf("%d",&a);
	b=year(a);
	if(b==1)
		printf("%d is a Leapyear!",a);
	else
		printf("%d isn't a Leapyear!",a);
	return 0;
}

2.无返回值的自定义函数

#include "stdio.h"
void year(int n)
{
	if((n%4==0 && n%100!=0) ||n%400==0)
	{
		printf("%d is a leap year!",n);	
	}
	else
		printf("%d isn't a leap year!",n);
}
int main(){
	int a=0;
	printf("Please input a year :n");
	scanf("%d",&a);
	year(a);
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/675729.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号