源代码如下:
#include <stdio.h>
int main()
{
int year;
printf("输入年份: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0)
{
// 这里如果被 400 整数是闰年
if ( year%400 == 0)
printf("%d 是闰年", year);
else
printf("%d 不是闰年", year);
}
else
printf("%d 是闰年", year );
}
else
printf("%d 不是闰年", year);
return 0;
}
扩展资料
1、判断输入的年份是否为闰年,自定义函数 leap() 来进行判断。该函数的核心内容就是闰年的判断条件即能被 4 整除但不能被 100 整除,或能被 400 整除。
2、求输入日期距 2011 年 1 月 1 日有多少天。首先判断 2011 年距输入的年份有多少年,这其中有多少年是闰年就将 sum 加多少个 366,有多少年是平年便将 sum 加上多少个 365。
定义一个12个月的每月天数数组,根据输入的年号判断其是否为闰年,并根据判断结果对2月份的天数给予修正。然后以输入的月份数为下标直接输出数组元素即为所求每月天数。代码如下:
#include "stdio.h"运行举例如下图:
#include "stdio.h"
int main()
{
/*****************程序十六********************************************/
int iMonth=0,iDay=0,iYear=0;
int iDayPR=0;
printf("Enter the month you want to know the days: \n");
printf("Please enter the year: \n");
printf("Year:");
scanf("%d",&iYear);
printf("Month:");
scanf("%d",&iMonth);
switch (iMonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
iDay=31;
break;
case 4:
case 6:
case 9:
case 11:
iDay=30;
break;
case 2:
iDay=1;
break;
default:
iDay=-1;
break;
}
if(iDay==1)
{
if (iYear%4==0)
{
if (iYear%100 !=0)
{
iDayPR=29;
}
else
{
iDayPR=(iYear%400==0)? 29:28;
}
}
else
{
iDayPR=28;
}
printf("%d year %d month has %d days.\n",iYear,iMonth,iDayPR);
}
else
{
if(iDay==-1)
{
printf("There is a error with you enter!\n");
}
else
{
printf("%d year %d month has %d days.\n",iYear,iMonth,iDay);
}
}
return 0;