编写一个用数组计算天数的c语言
时间: 2023-12-06 07:05:29 浏览: 44
以下是一个简单的 C 语言程序,使用数组计算给定日期是一年中的第几天:
```c
#include <stdio.h>
int main()
{
int month, day, i, days = 0;
int days_in_month[] = {31,28,31,30,31,30,31,31,30,31,30,31};
printf("Enter the month and day (in the format MM DD): ");
scanf("%d %d", &month, &day);
for (i = 0; i < month - 1; i++) {
days += days_in_month[i];
}
days += day;
printf("The day is the %dth day of the year.\n", days);
return 0;
}
```
该程序将用户输入的月份和日子存储在变量 `month` 和 `day` 中。`days_in_month` 数组存储了每个月的天数。程序使用一个循环来计算给定日期之前的天数,并将其存储在变量 `days` 中。最后,程序将该天是该年中的第几天输出到屏幕上。
相关问题
用c语言编写同一年两个日期相差天数
以下是使用C语言编写的同一年两个日期相差天数的程序:
```c
#include <stdio.h>
int main() {
int year, month1, day1, month2, day2;
int days_in_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int total_days = 0;
printf("Enter year: ");
scanf("%d", &year);
printf("Enter first date (mm dd): ");
scanf("%d %d", &month1, &day1);
printf("Enter second date (mm dd): ");
scanf("%d %d", &month2, &day2);
// Check for leap year
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days_in_month[2] = 29;
}
// Calculate days between dates
if (month1 == month2) {
total_days = day2 - day1;
} else {
total_days += days_in_month[month1] - day1;
for (int i = month1 + 1; i < month2; i++) {
total_days += days_in_month[i];
}
total_days += day2;
}
printf("Total days between dates: %d\n", total_days);
return 0;
}
```
该程序首先要求用户输入年份以及两个日期,然后检查是否为闰年,并使用每个月的天数数组来计算两个日期之间的天数差异。最后,程序将结果输出到控制台。
计算天数c语言编程pta
以下是一个使用C语言编写的计算天数的程序示例:
```c
#include <stdio.h>
int main() {
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31,30, 31};
int year, month, day;
int i, result = 0;
scanf("%d %d %d", &year, &month, &day);
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
days[1] = 29;
}
for (i = 0; i < month - 1; i++) {
result += days[i];
}
result += day;
printf("%04d-%02d-%02d:%d\n", year, month, day, result);
return 0;
}
```
这个程序首先定义了一个包含每个月天数的数组`days`,然后通过输入获取年、月、日的值。接下来,根据输入的年份判断是否是闰年,如果是闰年,则将二月的天数设为29天。然后使用一个循环累加每个月的天数到`result`变量中。最后,将结果以指定的格式输出。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)