Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

โปรแกรมแปลงจำนวนวันที่กำหนดเป็นปี สัปดาห์และวันใน 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("\nweeks = %d", weeks);
   printf("\ndays = %d ",days);
}
int main() {
   int Total_days = 209;
   find(Total_days);
   return 0;
}

ผลลัพธ์

หากเราเรียกใช้โค้ดข้างต้น จะเกิดผลลัพธ์ดังต่อไปนี้

years = 0
weeks = 29
days = 6