Write a C program to input number of days from user and convert it to years, weeks and days. How to convert days to years, weeks in C programming. Logic to convert days to years, weeks and days in C program.
Example
Input
Input
Enter days: 373
Output
373 days = 1 year/s, 1 week/s and 1 day/s
Required knowledge
Arithmetic operators, Data types, Basic input/output
Days conversion formula
1 year = 365 days (Ignoring leap year)
1 week = 7 days
Using this we can define our new formula to compute years and weeks.
year = days / 365
week = (days - (year * 365)) / 7
Logic to convert days to years weeks and days
Step by step descriptive logic to convert days to years, weeks and remaining days –
- Input days from user. Store it in some variable say days.
- Compute total years using the above conversion table. Which is
years = days / 365
. - Compute total weeks using the above conversion table. Which is
weeks = (days - (year * 365)) / 7
. - Compute remaining days using
days = days - ((years * 365) + (weeks * 7))
. - Finally print all resultant values years, weeks and days.
Program to convert days to years, weeks and days
/**
* C program to convert days in to years, weeks and days
*/
#include <stdio.h>
int main()
{
int days, years, weeks;
/* Input total number of days from user */
printf("Enter days: ");
scanf("%d", &days);
/* Conversion */
years = (days / 365); // Ignoring leap year
weeks = (days % 365) / 7;
days = days - ((years * 365) + (weeks * 7));
/* Print all resultant values */
printf("YEARS: %d\n", years);
printf("WEEKS: %d\n", weeks);
printf("DAYS: %d", days);
return 0;
}
Output
Enter days: 373 YEARS: 1 WEEKS: 1 DAYS: 1
Happy coding 😉
Recommended posts
- Basic programming exercises index.
- C program to convert length from centimeter to meter and kilometer.
- C program to convert temperature from Celsius to Fahrenheit.
- C program to convert temperature from Fahrenheit to Celsius.
- C program to calculate power of two numbers x^y.
- C program to calculate Simple Interest.