判断闰年的C程序

标签 c date

如何调整这段代码的输出:

  • 假设我在 2012 年输入。输出应该是 February 29, 2012 Wednesday is a leap year (same thing with other leap year)

  • 如果我输入 2013,输出应该是 2013 不是闰年。

这是我当前的代码:

include <stdio.h>
int main()
{
    int year;
    printf("Enter a year to check if it is a leap year\n");
    scanf("%d", &year);
    if ( year%400 == 0)
        printf("%d is a leap year.\n", year);
    else if ( year%100 == 0)
        printf("%d is not a leap year.\n", year);
    else if ( year%4 == 0 )
        printf("%d is a leap year.\n", year);
    else
        printf("%d is not a leap year.\n", year);
    return 0;
}

闰年的2月29日是星期几怎么判断?

最佳答案

你的问题让我想自己得到它 并开始对其进行研究和编写代码。我找到了一段代码,可以通过输入日期模式返回一天。在您的情况下,如果它是闰年,则日期为 29,2,year,如果不是,则为 28,2,year。这里 0 代表星期日,1 代表星期一,同样 6 代表星期六。

/* A program to find day of a given date */
#include<stdio.h>

int dayofweek(int d, int m, int y)
{
    static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
    y -= m < 3;
    return ( y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}

/* Driver function to test above function */
int main()
{
    int day = dayofweek(10, 10, 2014);
    printf ("%d", day);

    return 0;
}

如果您发现代码中有任何错误或难以对其进行更改。请让我知道我会尽力提供帮助。在您的代码中添加此代码可能会满足您的需求。

关于判断闰年的C程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26302793/

相关文章:

c - 在使用 'IF' 语句前进后,有没有办法返回?

bash - 更新文件中多个日期的日期格式

php - 如何在 PHP 中重新格式化日期时间字符串?

mysql - 如何编写 MySQL 存储过程来添加两个日期之间的一系列记录

R:把几个月变成季度

不能同时要求两个整数

c - 模块化编译时数组扩展

c - 指向数组元素的指针

c - 为什么会出现内存垃圾?

java - 检查字符串是否是有效日期