源代码如下:
#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"
int main(int argc,char *argv[]){
int y,m,a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
printf("Please enter y & m(int y>0, 0<m<13)...\n");
if(scanf("%d%d",&y,&m)!=2 || y<1 || m<1 || m>12){//输入年份和月份,保证正确
printf("Input error, exit...\n");
return 0;
}
a[1] = y%4==0 && y%100 || y%400==0 ? 29 :28;//判断y是否为闰年并调整a[1]的值
printf(a[1]-29 ? "\n%d is not " : "\n%d is ",y);
printf("a leap year, month%d is %d days.\n\n",m,a[m-1]);
return 0;
}
运行举例如下图:
2013-05-17
2013-05-17
#include<conio.h>
void main()
{
int year,month,day;
int flog;
scanf("%d,%d",&year,&month);
flog=1;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:day=31;break;
case 4:
case 6:
case 9:
case 11:day=30;break;
case 2: if( (year%4==0 &&(year%100)!=0)||year%400==0)
printf("this is leap year\n");
else
printf("this is not leap year\n");
break;
default :flog=0;
}
if(flog)
printf("%d",day=28);
else
printf("%d",day=29);
getch();
}
#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;