117.info
人生若只如初见

c语言怎么判断是闰年还是平年

要判断一个年份是否为闰年,可以遵循以下规则:

  1. 如果该年份能被4整除,但不能被100整除,则是闰年。
  2. 如果该年份能被400整除,则也是闰年。
  3. 其他情况下为平年。

在C语言中,可以使用如下代码来判断一个年份是否为闰年:

#include 

int main() {
    int year;
    printf("请输入一个年份:");
    scanf("%d", &year);

    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
        printf("%d年是闰年。", year);
    } else {
        printf("%d年是平年。", year);
    }

    return 0;
}

代码首先通过scanf函数获取用户输入的年份,然后使用if语句判断该年份是否为闰年,并输出相应的结果。

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe913AzsLAgFXDVE.html

推荐文章

  • C语言怎么读取外部配置文件

    在C语言中,可以使用文件操作函数来读取外部配置文件。下面是一个简单的示例代码:
    #include int main() { FILE *file; char buffer[100]; // 打开配置文件...

  • c语言怎么获取程序运行时间

    在C语言中,可以使用time.h头文件中的clock()函数来获取程序的运行时间。
    首先,在程序开始的地方调用clock()函数,将返回的时间戳保存在一个变量中,表示程...

  • c语言怎么获取文件夹下的所有文件名

    在C语言中,可以使用dirent.h头文件中的DIR和dirent结构体以及readdir函数来获取文件夹下的所有文件名。下面是一个简单的示例代码:
    #include #include int...

  • C语言怎么获取当前时间戳

    在C语言中,可以使用time.h头文件中的time()函数来获取当前时间戳。具体的代码如下:
    #include #include int main() { time_t currentTime; time(¤tT...

  • python怎么将大写字母转化为小写

    在Python中,可以使用lower()方法将大写字母转化为小写。以下是一个示例:
    string = "HELLO WORLD"
    lowercase_string = string.lower()
    print(lo...

  • 怎么用C语言读取串口数据

    要使用C语言读取串口数据,可以使用以下步骤: 引入头文件 #include #include #include #include #include 打开串口 int serial_fd = open("/dev/ttyS0", O_RDWR...

  • 怎么用c语言判断大小写字母

    可以使用C语言中的字符函数isupper和islower来判断一个字符是大写字母还是小写字母。下面是一个示例代码:
    #include #include int main() { char ch; print...

  • python闰年的计算方法是什么

    Python中判断闰年的计算方法如下:
    def is_leap_year(year): if year % 4 == 0: if year % 100 == 0: if year % 400 == 0: return True else: return False...