【C語言】輸入一個年份和月份,輸出該月的天數


分析:

三種類型,

A.2月比較特殊,平年的2月只有28天,而閏年的2月有 29 天;

B.4、6、9、11月;

C.其他1、3、5、7、8、10、12月。

代碼:

/*輸入一個年份和月份,輸出該月的天數*/
#include<stdio.h>
int main()
{
    int year, month;
    printf("輸入年和月(用空格分隔):\n");
    scanf_s("%d %d", &year, &month);
    switch (month)
    {
    case 2:if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
        printf("29天!\n");
    else
        printf("28天!\n");
    break;
    case 4:
    case 6:
    case 9:
    case 11:printf("30天!\n"); break;
    default:printf("31天!\n"); break;
    }
    return 0;
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM