Given number of days, convert it in terms of Years, Week and Days.
Examples :
Input : 30
Output : years = 0
week = 4
days = 2
Input : 20
Output : years = 0
week = 2
days = 6
Approach :
Number of years will be the quotient when number of days will be divided by 365 i.e days / 365 = years. Number of weeks will be the result of (Number_of_days % 365) / 7. Number of days will be the result of (Number_of_days % 365) % 7.
Below is the program implementing above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define DAYS_IN_WEEK 7
void find(int number_of_days)
{
int year, week, days;
year = number_of_days / 365;
week = (number_of_days % 365) /
DAYS_IN_WEEK;
days = (number_of_days % 365) %
DAYS_IN_WEEK;
cout << "years = " << year;
cout << "\nweeks = " << week;
cout << "\ndays = " << days;
}
int main()
{
int number_of_days = 200;
find(number_of_days);
return 0;
}
C
#include <stdio.h>
#define DAYS_IN_WEEK 7
void find(int number_of_days)
{
int year, week, days;
year = number_of_days / 365;
week = (number_of_days % 365) /
DAYS_IN_WEEK;
days = (number_of_days % 365) %
DAYS_IN_WEEK;
printf("years = %d",year);
printf("\nweeks = %d", week);
printf("\ndays = %d ",days);
}
int main()
{
int number_of_days = 200;
find(number_of_days);
return 0;
}
Java
class GFG
{
static final int DAYS_IN_WEEK = 7;
static void find(int number_of_days)
{
int year, week, days;
year = number_of_days / 365;
week = (number_of_days % 365) /
DAYS_IN_WEEK;
days = (number_of_days % 365) %
DAYS_IN_WEEK;
System.out.println("years = " + year);
System.out.println("weeks = " + week);
System.out.println("days = " + days);
}
public static void main(String[] args)
{
int number_of_days = 200;
find(number_of_days);
}
}
Python3
DAYS_IN_WEEK = 7
def find( number_of_days ):
year = int(number_of_days / 365)
week = int((number_of_days % 365) /
DAYS_IN_WEEK)
days = (number_of_days % 365) % DAYS_IN_WEEK
print("years = ",year,
"\nweeks = ",week,
"\ndays = ",days)
number_of_days = 200
find(number_of_days)
C#
using System;
class GFG
{
static int DAYS_IN_WEEK = 7;
static void find(int number_of_days)
{
int year, week, days;
year = number_of_days / 365;
week = (number_of_days % 365) /
DAYS_IN_WEEK;
days = (number_of_days % 365) %
DAYS_IN_WEEK;
Console.WriteLine("years = " +
year);
Console.WriteLine("weeks = " +
week);
Console.WriteLine("days = " +
days);
}
public static void Main()
{
int number_of_days = 200;
find(number_of_days);
}
}
PHP
<?php
$DAYS_IN_WEEK = 7;
function find($number_of_days)
{
global $DAYS_IN_WEEK;
$year; $week; $days;
$year = (int)($number_of_days / 365);
$week = (int)(($number_of_days % 365) /
$DAYS_IN_WEEK);
$days = ($number_of_days % 365) %
$DAYS_IN_WEEK;
echo("years = " . $year .
"\nweeks = " . $week .
"\ndays = " . $days);
}
$number_of_days = 200;
find($number_of_days);
?>
Javascript
<script>
var DAYS_IN_WEEK = 7;
function find(number_of_days) {
var year, week, days;
year = parseInt(number_of_days / 365);
week = parseInt((number_of_days % 365) / DAYS_IN_WEEK);
days = (number_of_days % 365) % DAYS_IN_WEEK;
document.write("years = " + year + "<br/>");
document.write("weeks = " + week + "<br/>");
document.write("days = " + days + "<br/>");
}
var number_of_days = 200;
find(number_of_days);
</script>
Output :
years = 0
weeks = 28
days = 4
Time complexity: O(1)Auxiliary space: O(1)
Last Updated :
20 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...