C PROGRAMMING PROGRAM • BASICS & INPUT/OUTPUT

Convert Days into Years, Weeks and Days

Learn how to convert days into years, weeks and days using a clear C program.

BeginnerInteger divisionRemainderUnit conversion

PROBLEM UNDERSTANDING

Input and expected output

Sample input
800
Sample output
2 year(s), 10 week(s), 0 day(s)

COMPLETE C PROGRAM

Complete C implementation

days-to-years-weeks-days.c
Open in compiler
#include <stdio.h>

int main(void)
{
    int totalDays, years, weeks, days;

    printf("Enter total days: ");
    scanf("%d", &totalDays);
    years = totalDays / 365;
    totalDays %= 365;
    weeks = totalDays / 7;
    days = totalDays % 7;
    printf("%d year(s), %d week(s), %d day(s)\n", years, weeks, days);
    return 0;
}

GUIDED CODE TOUR • NOT LIVE EXECUTION

Study the program line by line

Use the real compiler button above to run and debug with different inputs.

CURRENT STEP

Select Start to walk through the important lines.

SELECTED LINE

No line selected

EXPECTED OUTPUT FOR THE SAMPLE

2 year(s), 10 week(s), 0 day(s)
0%Step 0 of 0

PROGRAM EXPLANATION

Algorithm and explanation

  1. Read the required input values.
  2. Repeatedly use integer division and remainder from the largest unit to the smallest.
  3. Display the computed result.

Repeatedly use integer division and remainder from the largest unit to the smallest.

EFFICIENCY

Time and space complexity

Time complexity

O(1)

Auxiliary space

O(1)

DEBUGGING CHECKLIST

Common mistakes

Check this

Use the correct format specifier for every variable.

Check this

Initialize variables before using their values.

Check this

Check braces, semicolons and input order carefully.

Try it yourself

Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.