Find the next identical calendar year

You are given an year Y, find the next identical calendar year to Y.

Examples :

Input : 2017
Output : 2023

Input : 2018
Output : 2029

An year x is identical to a given previous year y if following two conditions are satisfied.

  1. x starts with same day as y.
  2. If y is leap year, then x is also. If y is not leap year, then x is also not.

The idea is to check all years one by one (starting from next year). We keep track of number of days moved ahead. If total moved days is 7, then current year begins with same day. We also check if leap-ness of current year is same as y. If both conditions are satisfied, we return current year.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find next identical year
#include<iostream>
using namespace std;
  
// Function for finding extra days of year
// more than complete weeks
int extraDays(int y)
{
    // If current year is a leap year, then
    // it number of weekdays move ahead by
    // 2 in terms of weekdays.
    if (y%400==0 || y%100!=0 && y%4==0)
        return 2;
  
    // Else number of weekdays move ahead
    // by 1.
    return 1;
}
  
// Returns next identical year.
int nextYear(int y)
{
    // Find number of days moved ahead by y
    int days = extraDays(y);
  
    // Start from next year
    int x = y + 1;
  
    // Count total number of weekdays
    // moved ahead so far.
    for (int sum=0; ; x++)
    {
        sum = (sum + extraDays(x)) % 7;
  
        // If sum is divisible by 7 and leap-ness
        // of x is same as y, return x.
        if ( sum==0 && (extraDays(x) == days))
            return x;
    }
  
    return x;
}
  
// driver program
int main()
{
    int y = 2018;
    cout << nextYear(y);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find next identical year
class GFG {
  
// Function for finding extra days of year
// more than complete weeks
static int extraDays(int y)
{
    // If current year is a leap year, then
    // it number of weekdays move ahead by
    // 2 in terms of weekdays.
    if (y % 400 == 0 || y % 100 != 0 && y % 4 == 0)
        return 2;
  
    // Else number of weekdays move ahead
    // by 1.
    return 1;
}
  
// Returns next identical year.
static int nextYear(int y)
{
    // Find number of days moved ahead by y
    int days = extraDays(y);
  
    // Start from next year
    int x = y + 1;
  
    // Count total number of weekdays
    // moved ahead so far.
    for (int sum = 0; ; x++)
    {
        sum = (sum + extraDays(x)) % 7;
  
        // If sum is divisible by 7 and leap-ness
        // of x is same as y, return x.
        if ( sum == 0 && (extraDays(x) == days))
            return x;
    }
  
}
  
// Driver code
public static void main(String[] args)
{
    int y = 2018;
    System.out.println(nextYear(y));
}
}
  
/* This code contributed by PrinciRaj1992 */

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find next identical year
using System;
      
class GFG 
{
  
// Function for finding extra days of year
// more than complete weeks
static int extraDays(int y)
{
    // If current year is a leap year, then
    // it number of weekdays move ahead by
    // 2 in terms of weekdays.
    if (y % 400 == 0 || y % 100 != 0 && y % 4 == 0)
        return 2;
  
    // Else number of weekdays move ahead
    // by 1.
    return 1;
}
  
// Returns next identical year.
static int nextYear(int y)
{
    // Find number of days moved ahead by y
    int days = extraDays(y);
  
    // Start from next year
    int x = y + 1;
  
    // Count total number of weekdays
    // moved ahead so far.
    for (int sum = 0; ; x++)
    {
        sum = (sum + extraDays(x)) % 7;
  
        // If sum is divisible by 7 and leap-ness
        // of x is same as y, return x.
        if ( sum == 0 && (extraDays(x) == days))
            return x;
    }
  
}
  
// Driver code
public static void Main(String[] args)
{
    int y = 2018;
    Console.WriteLine(nextYear(y));
}
}
  
// This code has been contributed by 29AjayKumar

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find 
// next identical year
  
// Function for finding extra days 
// of year more than complete weeks
  
function extraDays($y)
{
    // If current year is a leap year, 
    // then number of weekdays move 
    // ahead by 2 in terms of weekdays.
    if ($y % 400 == 0 || 
        $y % 100 != 0 &&
        $y % 4 == 0)
        return 2;
  
    // Else number of weekdays 
    // move ahead by 1.
    return 1;
}
  
// Returns next identical year.
function nextYear($y)
{
    // Find number of days
    // moved ahead by y
    $days = extraDays($y);
  
    // Start from next year
    $x = $y + 1;
  
    // Count total number of weekdays
    // moved ahead so far.
    for ($sum = 0; ; $x++)
    {
        $sum = ($sum + extraDays($x)) % 7;
  
        // If sum is divisible by 7 
        // and leap-ness of x is 
        // same as y, return x.
        if ( $sum == 0 && (extraDays($x) == $days))
            return $x;
    }
  
    return $x;
}
  
// Driver Code
$y = 2018;
echo nextYear($y);
  
// This code is contributed by aj_36
?>

chevron_right



Output :



2029

This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.