从键盘输入你的出生年和今年的年份,编程判断并输出从你的出生年到今年之间中有多少个闰年。用基本的c语言代码
时间: 2024-03-19 15:46:08 浏览: 23
好的,以下是使用 C 语言编写的程序,计算从指定的出生年到今年之间的闰年个数。程序思路与上面 Python 语言的实现类似。
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
int birth_year, this_year;
int leap_year_count = 0;
printf("Input your birth year:");
scanf("%d", &birth_year);
printf("Input this year:");
scanf("%d", &this_year);
for (int year = birth_year; year <= this_year; year++) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
printf("%d ", year);
leap_year_count++;
}
}
printf("\ncount=%d", leap_year_count);
return 0;
}
```
程序中使用了 `for` 循环来遍历从指定的出生年到今年之间的年份。如果当前年份是闰年,就输出该年份并将闰年计数器加1。最后输出闰年的个数。注意,需要使用 `<stdlib.h>` 头文件中的 `scanf` 函数来读取用户输入的年份。