Q:

C program to convert a given number of days into days, weeks, and years

0

C program to convert a given number of days into days, weeks, and years

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Read the number of days from the user and convert it to years, weeks, and days using C program.

Program:

The source code to convert the given number of days into days, weeks, and years is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to convert a given number of days into
// days, weeks, and years

#include <stdio.h>

int main()
{
    int ndays = 0;
    int years = 0;
    int weeks = 0;
    int days = 0;

    printf("Enter days: ");
    scanf("%d", &ndays);

    years = ndays / 365;
    weeks = (ndays % 365) / 7;
    days = (ndays % 365) % 7;

    printf("%d years, %d weeks and %d days\n", years, weeks, days);

    return 0;
}

Output:

RUN 1:
Enter days: 367
1 years, 0 weeks and 2 days

RUN 2:
Enter days: 1280
3 years, 26 weeks and 3 day

RUN 3:
Enter days: 11
0 years, 1 weeks and 4 days

Explanation:

In the above program, we created 4 integer variables ndaysyearsweeksdays that are initialized with 0. Then we read the number of days from the user and convert it into years, weeks, days, and printed them on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now