1. 程式人生 > 其它 >C語言求某一年的某一月有多少天問題

C語言求某一年的某一月有多少天問題

First day!

此期間使用的是 Visual Studio 2019

 

#define _CRT_SECURE_NO_WARNINGS //vs 2019 scanf不安全警告


//方法一 if else
#include<stdio.h>
int main()
{
int a,b;
printf("請輸入日期:");
scanf("%d %d", & a, & b);

if (b ==1 || b == 3 || b == 5 || b == 7 || b == 8 || b == 10 || b == 12)
printf("%d年%d月有31天", a, b);
else if(b == 4 || b == 6 || b == 9 ||b == 11 )
printf("%d年%d月有30天", a, b);

else if ((a % 4 == 0 && a % 100 != 0) || (a % 400 == 0))

printf("%d年%d月有28天", a, b);
else
printf("%d年%d月有29天", a, b);
return 0;

 

//方法二 Switch Case

#include<stdio.h>
int main()
{
int a,b;
printf("請輸入日期:");
scanf("%d %d", & a, & b);
switch (b)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf("%d年%d月有31天,a,b"); break;

case 4:
case 6:
case 9:
case 11:
printf("%d年%d月有30天,a,b"); break;

case 2:
if ((a % 4 == 0 && a % 100 != 0) || (a % 400 == 0))

printf("%d年%d月有28天", a, b);
else
printf("%d年%d月有29天", a, b);
}