【基础学习】C 计算闰年

闰年计算逻辑:能够被 4 整除的年份是闰年,但其中能够被 100 整除的却不是闰年,除非它同时能被 400 整除。

实现逻辑:

#include <stdio.h>

int is_leap_year(int a) {
    int leap_year = 0;

    if (a % 4 == 0)
    {
        leap_year = 1;

        if (a % 100 ==0)
        {
            leap_year = 0;

            if (a % 400 == 0)
            {
                leap_year = 1;
            }
        }
    }

    return leap_year;
}

int main() {
    int a[6] = {2000, 1990, 2010, 1900, 2012, 2002};

    for (int i = 0; i <6 ; ++i)
    {
        if (is_leap_year(a[i]))
        {   
            printf("%d is leap year\n", a[i]);
        } else {
            printf("%d is not leap year\n", a[i]);
        }
    }

    return 0;
}

运行:

➜  C ./a.out 
2000 is leap year
1990 is not leap year
2010 is not leap year
1900 is not leap year
2012 is leap year
2002 is not leap year
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容