w3resource logo


C Programming Exercises

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

Secondary Nav

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
Expected Output :
Years: 3
Weeks: 33
Days: 3

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;
}

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.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program