ADVERTISEMENT
Convert Given Days to Years, Week and Days
This code takes a user input of a total number of days and breaks it down into the equivalent number of years, weeks, and days. It performs the calculations and provides the result in a human-readable format.
#include<stdio.h>
main()
{
int nodays,years,weeks,days;
printf("Enter the total days\n");
scanf("%d",&nodays);
years=nodays/365;
weeks=(nodays%365)/7;
days=(nodays%365)%7;
printf("%d = %d years,%d weeks,%d days\n",nodays,years,weeks,days);
}C Programming ExamplesExplanation:
This C program takes an input of the total number of days and then calculates and displays the equivalent number of years, weeks, and days. Here’s a step-by-step explanation of the code:
Header File:
- The program includes the standard C library header <stdio.h>, which is necessary for input and output operations.
Main Function:
- The main function is the entry point of the program, where the execution starts.
Variable Declarations:
- The program declares four integer variables: nodays, years, weeks, and days. These variables are used to store the total number of days and the calculated number of years, weeks, and days.
User Input:
- The program prompts the user with the message “Enter the total days” to enter the total number of days.
- The scanf function is used to read the user’s input, and the value is stored in the nodays variable.
Calculations:
The program calculates the number of years, weeks, and days based on the total number of days provided by the user. The calculation is performed as follows:
- years: The total number of days is divided by 365 to calculate the number of years.
- weeks: The remaining days (after subtracting the number of days accounted for in years) are divided by 7 to calculate the number of weeks.
- days: The remaining days, after both years and weeks are accounted for, are assigned to the days variable.
Output:
The program uses printf to display the result in the following format:
- X = Y years, Z weeks, W days
- X represents the total number of days provided by the user.
- Y represents the number of years calculated.
- Z represents the number of weeks calculated.
- W represents the number of days calculated.
End of Program:
- After displaying the result, the program reaches the end of the main function and, subsequently, the end of the program’s execution.