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
示例
#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("
weeks = %d", weeks);
printf("
days = %d ",days);
}
int main() {
int Total_days = 209;
find(Total_days);
return 0;
}輸出
如果執行以上程式碼,將生成以下輸出
years = 0 weeks = 29 days = 6
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP