Program to calculate the number of odd days in given number of years
Last Updated :
15 Dec, 2022
Given an integer N, the task is to find the number of odd days in the years from 1 to N.
Odd Days: Number of odd days refer to those days that are left in a certain year(s) when it’s days gets converted into weeks. Say, an ordinary year has 365 days, that is 52 weeks and one odd day. This means, out of the 365 days in an ordinary year, 364 days will get converted into 52 weeks and one day will remain. This one day is referred to as 1 odd day.
- Simply the modulus total number of days by 7(days in a week) gives us the number of odd days.
- It’s value lies between 0 to 6 only. [0, 6]
- Leap Year: Every year divisible either by 400 or by 4 but not 100
- Ordinary Year: Years Except Leap Years
- Every Ordinary Year has 1 odd day.
- Every Leap Year has 2 odd days.
Examples:
Input: N = 8
Output: 3
Out of the 8 years, 4 and 8 are the only leap years.
(6 x 1) + (2 x 2) = 10 i.e. 1 week and 3 days
Input: N = 400
Output: 0
Approach:
- Count number of ordinary years and number of leap years out of given N years.
- Calculate the overall number of days.
- Print the modulo(7) of the total number of days.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int oddDays( int N)
{
int hund1 = N / 100;
int hund4 = N / 400;
int leap = N >> 2;
int ord = N - leap;
if (hund1) {
ord += hund1;
leap -= hund1;
}
if (hund4) {
ord -= hund4;
leap += hund4;
}
int days = ord + leap * 2;
int odd = days % 7;
return odd;
}
int main()
{
int N = 100;
cout << oddDays(N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int oddDays( int N)
{
int hund1 = N / 100 ;
int hund4 = N / 400 ;
int leap = N >> 2 ;
int ord = N - leap;
if (hund1 > 0 ) {
ord += hund1;
leap -= hund1;
}
if (hund4 > 0 ) {
ord -= hund4;
leap += hund4;
}
int days = ord + leap * 2 ;
int odd = days % 7 ;
return odd;
}
public static void main(String args[])
{
int N = 100 ;
System.out.print(oddDays(N));
}
}
|
Python3
def oddDays(N):
hund1 = N / / 100
hund4 = N / / 400
leap = N >> 2
ordd = N - leap
if (hund1):
ordd + = hund1
leap - = hund1
if (hund4):
ordd - = hund4
leap + = hund4
days = ordd + leap * 2
odd = days % 7
return odd
N = 100
print (oddDays(N))
|
C#
using System;
class GFG
{
static int oddDays( int N)
{
int hund1 = N / 100;
int hund4 = N / 400;
int leap = N >> 2;
int ord = N - leap;
if (hund1 > 0)
{
ord += hund1;
leap -= hund1;
}
if (hund4 > 0)
{
ord -= hund4;
leap += hund4;
}
int days = ord + leap * 2;
int odd = days % 7;
return odd;
}
static void Main()
{
int N = 100;
Console.WriteLine(oddDays(N));
}
}
|
PHP
<?php
function oddDays( $N )
{
$hund1 = floor ( $N / 100);
$hund4 = floor ( $N / 400);
$leap = $N >> 2;
$ord = $N - $leap ;
if ( $hund1 )
{
$ord += $hund1 ;
$leap -= $hund1 ;
}
if ( $hund4 )
{
$ord -= $hund4 ;
$leap += $hund4 ;
}
$days = $ord + $leap * 2;
$odd = $days % 7;
return $odd ;
}
$N = 100;
echo oddDays( $N );
?>
|
Javascript
<script>
function oddDays(N) {
var hund1 = N / 100;
var hund4 = N / 400;
var leap = N >> 2;
var ord = N - leap;
if (hund1 > 0) {
ord += hund1;
leap -= hund1;
}
if (hund4 > 0) {
ord -= hund4;
leap += hund4;
}
var days = ord + leap * 2;
var odd = days % 7;
return odd;
}
var N = 100;
document.write(oddDays(N).toFixed());
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...