C语言 计算平闰年,某个月有多少天

#include <stdio.h>

int isLeapYear(int year) {
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        return 1;  // 闰年返回1
    return 0;  // 平年返回0
}

int getDaysInMonth(int year, int month) {
    int days;
    switch (month) {
        case 2:
            days = isLeapYear(year) ? 29 : 28;  // 2月根据平闰年返回天数
            break;
        case 4: case 6: case 9: case 11:
            days = 30;  // 4, 6, 9, 11月有30天
            break;
        default:
            days = 31;  // 其他月份有31天
    }
    return days;
}

int main() {
    int year, month;
    printf("输入年份: ");
    scanf("%d", &year);  // 获取年份
    printf("输入月份: ");
    scanf("%d", &month);  // 获取月份

    printf("%d年%d月有%d天\n", year, month, getDaysInMonth(year, month));  // 输出结果

    return 0;
}

说明
  1. 函数isLeapYear用于判断是否为闰年。
  2. 函数getDaysInMonth根据年份和月份返回对应的天数。
  3. main函数中获取用户输入的年份和月份,调用getDaysInMonth并输出结果。
  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值