首页  >  文章  >  后端开发  >  编写一个C程序,将给定的天数转换为年、周和天

编写一个C程序,将给定的天数转换为年、周和天

WBOY
WBOY转载
2023-09-01 23:45:07591浏览

编写一个C程序,将给定的天数转换为年、周和天

给定了天数,任务是将给定的天数转换为年、周和天。

让我们假设一年中的天数 =365

年数=(天数)/365

解释-:年数将是除以给定天数得到的商与 365

周数 = (天数 % 365) / 7

解释-:周数将通过收集余数获得将天数除以 365,再除以一周的天数 7。

天数 = (天数 % 365) % 7

说明-:天数是用天数除以365所得的余数再除以一周的天数7得到的余数。

示例

Input-:days = 209
Output-: years = 0
   weeks = 29
   days = 6
Input-: days = 1000
Output-: years = 2
   weeks = 38
   days = 4

算法

Start
Step 1-> declare macro for number of days as const int n=7
Step 2-> Declare function to convert number of days in terms of Years, Weeks and Days
   void find(int total_days)
      declare variables as int year, weeks, days
      Set year = total_days / 365
      Set weeks = (total_days % 365) / n
      Set days = (total_days % 365) % n
      Print year, weeks and days
Step 3-> in main()
   Declare int Total_days = 209
   Call find(Total_days)
Stop

Example

 现场演示

#include <stdio.h>
const int n=7 ;
//find year, week, days
void find(int total_days) {
   int year, weeks, days;
   // assuming its not a leap year
   year = total_days / 365;
   weeks = (total_days % 365) / n;
   days = (total_days % 365) % n;
   printf("years = %d",year);
   printf("</p><p>weeks = %d", weeks);
   printf("</p><p>days = %d ",days);
}
int main() {
   int Total_days = 209;
   find(Total_days);
   return 0;
}

输出

如果我们运行上述代码,它将生成以下输出

years = 0
weeks = 29
days = 6

以上是编写一个C程序,将给定的天数转换为年、周和天的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除