w3resource logo
C Programming Exercises

C Exercises: Convert specified days into years, weeks and days

C Basic Declarations and Expressions: Exercise-8 with Solution

Write a C program to convert specified days into years, weeks and days.
Note: Ignore leap year.
Test Data :
Number of days : 1329

C Code:

#include <stdio.h> 
int main()
{
    int days, years, weeks;

    days = 1329; 

    // Converts days to years, weeks and days
    years = days/365; 
    weeks = (days % 365)/7;
    days = days- ((years*365) + (weeks*7));

    printf("Years: %d\n", years);
    printf("Weeks: %d\n", weeks);
    printf("Days: %d \n", days);

    return 0;
}

Sample Output:

Years: 3                                                               
Weeks: 33                                                              
Days: 3

Solution

#include <stdio.h> 
int main()
{
    int days, years, weeks;

    days = 1329; 

    // Converts days to years, weeks and days
    years = days/365; 
    weeks = (days % 365)/7;
    days = days- ((years*365) + (weeks*7));

    printf("Years: %d\n", years);
    printf("Weeks: %d\n", weeks);
    printf("Days: %d \n", days);

    return 0;
}

Improve this sample solution and post your code through Disqus