# C Exercises: Convert a given integer to years, months and days

## C Basic Declarations and Expressions: Exercise-18 with Solution

Write a C program to convert a given integer (in days) to years, months and days, assuming that all months have 30 days and all years have 365 days.

**C Code:**

```
#include <stdio.h>
int main() {
int ndays, y, m, d; // Declare variables for number of days, years, months, and days
// Prompt user for input number of days and store in 'ndays'
printf("Input no. of days: ");
scanf("%d", &ndays);
// Calculate years, months, and remaining days
y = (int) ndays/365;
ndays = ndays-(365*y);
m = (int)ndays/30;
d = (int)ndays-(m*30);
// Print the result
printf(" %d Year(s) \n %d Month(s) \n %d Day(s)", y, m, d);
return 0;
}
```

Sample Output:

Input no. of days: 2535 6 Year(s) 11 Month(s) 15 Day(s)

**Flowchart:**

**C Programming Code Editor:**

**Previous:** Write a C program to convert a given integer (in seconds) to hours, minutes and seconds.

**Next:** Write a C program that accepts 4 integers p, q, r, s from the user where q, r and s are positive and p is even. If q is greater than r and s is greater than p and if the sum of r and s is greater than the sum of p and q print "Correct values", otherwise print "Wrong values".

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

**Weekly Trends**- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join