C语言:给出年份和月份,计算并显示该年该月的天数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言:给出年份和月份,计算并显示该年该月的天数相关的知识,希望对你有一定的参考价值。
参考技术A #includeint
main()
int
year,month,day;
printf("请输入年份与月份:");
scanf("%d",&year);
scanf("%d",&month);
if(year%4==0)
if(year%100==0)
if(year%400==0)
printf("%d是闰年\n",year);
else
printf("%d不是闰年\n",year);
else
printf("%d是闰年\n",year);
else
printf("%d不是闰年\n",year);
switch
(month)
case
1:
case
2:
case
3:
printf("%d是春季。\n",month);
break;
case
4:
case
5:
case
6:
printf("%d是夏季。\n",month);
break;
case
7:
case
8:
case
9:
printf("%d是秋季。\n",month);
break;
case
10:
case
11:
case
12:
printf("%d是冬季。\n",month);
break;
default:
printf("输入错误.\n");
if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||
(month==12))
printf("该月为31天!\n");
if((month==4)||(month==6)||(month==9)||(month==11))
printf("该月为30天!\n");
if(month==2)
if(year%4==0)
if(year%100==0)
if(year%400==0)
printf("该月29天");
else
printf("该月28天");
else
printf("该月29天\n");
else
printf("该月28天\n");
return
0;
挑战C语言
判断年月
先确定好该年是否闰年再写程序
#include<stdio.h>
void main(){
int year;
printf("Please enter year:");
scanf("%d",&year);
if(year%4==0&&year>=0){
if(year%100==0){
if(year%400==0)
printf("%d is leap year\n",year);
else
printf("%d is not leap year\n",year);
}
else
printf("%d is leap year\n",year);
}
else
printf("%d is not leap year\n",year);
}
void main()
{
int year,month,leap;//整数数据类型
printf("Please enter year,month:");//输出
scanf("%d,%d",&year,&month);//输入“year,month”前后数字中间加逗号
//leap year,leap=1代表闰年,leap=0代表平年(假设,1真,0假)
if(year%4==0&&year>=0)//判断数字是否被4整除以及大于0
{
if(year%100==0)//如果数字能被4整除并且大于0,再判断数字是否被100整除
{
if(year%400==0)//如果数字能被100整除,再判断是否被400整除
leap=1;//如果数字能被400整除(能被4整除,能被100整除),那数字1将赋值到变量leap
else
leap=0;//如果数字不能被400整除,能被4整除,能被100整除,那数字0将赋值到变量leap
}
else
leap=1;//如果数字不能被100整除并且能被4整除,那数字1将赋值到变量leap
}
else
leap=0;//如果数字不能被4整除,那数字0将赋值到变量leap
//是否闰年
if(leap==1)//判断
printf("%d is leap year\n",year);//如果leap等于1,那么输出是闰年
else//否则
printf("%d is not leap year\n",year); //如果leap不等于1,那么输出不是闰年
//spring:3,4,5;
//summer:6,7,8;
//autumn:9,10,11;
//winter:12,1,2;
switch(month)//判断选择,该月属于哪一季节
{
case 1: //month=1时,空的,下一句执行
case 2: printf("The season is winter\n");break; //month=2时输出“The season is spring”,break不可省略
//同理
case 3:
case 4:
case 5: printf("The season is spring\n");break;
case 6:
case 7:
case 8: printf("The season is summer\n");break;
case 9:
case 10:
case 11: printf("The season is autumn\n");break;
case 12:printf("The season is winter\n");break;
default:printf("The month is not exist\n");//default,不成立的时候输出“The month is not exist”
}
//31days:1,3,5,7,8,10,12;
//30days:4,6,9,11;
//leap year 29days:2;闰年中2月有29天
//not leap year 28days:2;平年中2月有28天
switch(month)//再判断选择,判断该月的天数
{
case 1:printf("The number of days of this month 31\n");break;//一月有31天
case 2:{ //2月比较特殊
if(leap==1)//判断
printf("The number of days of this month 29\n");//该年是闰年,输出2月有29天
else
printf("The number of days of this month 28\n");//该年是平年,输出是2月有28天
};break;
case 3:printf("The number of days of this month 31\n");break;
case 4:printf("The number of days of this month 30\n");break;
case 5:printf("The number of days of this month 31\n");break;
case 6:printf("The number of days of this month 30\n");break;
case 7:printf("The number of days of this month 31\n");break;
case 8:printf("The number of days of this month 31\n");break;
case 9:printf("The number of days of this month 30\n");break;
case 10:printf("The number of days of this month 31\n");break;
case 11:printf("The number of days of this month 30\n");break;
case 12:printf("The number of days of this month 31\n");break;
default:printf("The month is not exist\n");//不符合以上条件的时候就输出该季节不存在
}
}
以上是关于C语言:给出年份和月份,计算并显示该年该月的天数的主要内容,如果未能解决你的问题,请参考以下文章