溫馨提示×

C語言如何把天數(shù)轉(zhuǎn)化為年月

小億
239
2023-11-10 13:52:20
欄目: 編程語言

C語言可以通過除法和取余運算來將天數(shù)轉(zhuǎn)化為年月。

首先,將天數(shù)除以365得到年數(shù)。然后,將余數(shù)再除以30得到月數(shù)。最后,將余數(shù)再除以1得到剩余的天數(shù)。

以下是一個示例代碼:

#include <stdio.h>

void convertDaysToYearsMonths(int days, int *years, int *months, int *remainingDays) {
    *years = days / 365;
    *months = (days % 365) / 30;
    *remainingDays = (days % 365) % 30;
}

int main() {
    int days = 1000;
    int years, months, remainingDays;

    convertDaysToYearsMonths(days, &years, &months, &remainingDays);

    printf("%d days is equal to %d years, %d months, and %d days\n", days, years, months, remainingDays);

    return 0;
}

輸出結(jié)果為:

1000 days is equal to 2 years, 8 months, and 20 days

這樣,你可以根據(jù)需要將任意的天數(shù)轉(zhuǎn)化為年月。

0