HomeC ProgrammingC Program to Convert Days into Years, Months and Days

C Program to Convert Days into Years, Months and Days

This C program converts a given number of days into years, months, and remaining days. It uses a function called convertDays to perform the conversion.

Problem statement

Given a number of days, the program needs to determine the equivalent number of years, months, and remaining days.

C Program to Convert Days into Years, Months and Days

#include <stdio.h>

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

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

    // Input
    printf("Enter the number of days: ");
    scanf("%d", &days);

    // Conversion
    convertDays(days, &years, &months, &remainingDays);

    // Output
    printf("Years: %d\n", years);
    printf("Months: %d\n", months);
    printf("Days: %d\n", remainingDays);

    return 0;
}

How it works?

  1. The user enters the number of days.
  2. The convertDays function is called with the input days and the addresses of years, months, and remainingDays.
  3. Inside the convertDays function, the number of years is calculated by dividing the days by 365 (approximating a year to 365 days).
  4. The remaining days after considering the years are calculated by taking the modulus of days with 365.
  5. The number of months is calculated by dividing the remaining days by 30 (approximating a month to 30 days).
  6. The remaining days after considering the months are calculated by taking the modulus of remaining days with 30.
  7. The calculated values of years, months, and remaining days are stored in the respective variables.
  8. The program returns to the main function.
  9. The converted values of years, months, and remaining days are printed on the screen.

Input/Output

C Program to Convert Days into Years, Months and Days

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C program calculates the volume and surface area of a sphere using its radius. A sphere is a three-dimensional...
This C program converts a Roman numeral to a decimal number. Roman numerals are a system of numerical notation used...
This C program calculates the value of sin(x) using the Taylor series expansion. The Taylor series expansion is a mathematical...