C语言入门 -- 输出某个月有多少天(2020/12/9)

输出某个月有多少天(考虑闰年)

编写一个显示以下提示的程序:
输入年份:
输入月份(1表示一月,2表示二月,等等):
您的程序应该接受并存储一个名为year的变量以响应第一个提示,并在名为month的变量中接受并存储一个数字,以响应第二个提示。
程序应计算天数并将值存储在名为day的变量中,以响应输入的年和月。
如果输入的月份无效,程序应通知用户输入的月份无效。
【提示】
(1) 输入的月份应介于1到12之间;
(2) 一月、三月、五月、七月、八月、十月和十二月有31天;其他月份都有30天。
(3) 二月在非闰年有28天,闰年有29天。
(4) 要判断闰年,可以使用布尔表达式
(年份%4 == 0和年份%100!=0)| |(年%400==0)

/*Name:programme3.cAuthor:祁麟copyright:BJTU | school of soft ware class2004Date:2020/10/12 Description:Write a program that displays the following prompts :Enter a year:Enter a month (use a 1 for Jan., 2 for Feb., etc.):
*/#include <stdio.h>int main (){int year,month,day;printf ("Enter a year:\n");scanf ("%d",&year);printf ("Enter a month:\n");scanf ("%d",&month);switch (month) {case 1 :case 3 :case 5 :case 7 :case 8 : case 10:case 12:day = 31;printf ("day:%d\n",day);break;case 2:if  ( year%4==0 && year%100!=0 || year%400==0 )//能被4整除,且不能被100整除或能被400整除的年份为闰年 {day = 29;printf ("day:%d\n",day);} else{day = 28;printf ("day:%d\n",day);}break; case 4 :case 6 :case 9 :case 11:day = 30;printf ("day:%d\n",day);break;default:printf ("An invalid month is entered.");}return 0;} 

本文链接:https://my.lmcjl.com/post/1178.html

展开阅读全文

4 评论

留下您的评论.