
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the next identical calendar year in C++
Suppose we have an year Y. Find next identical calendar year to Y. So the calendar of 2017 is identical with 2023.
A year X is identical to given previous year Y if it matches these two conditions.
- x starts with the same day as year,
- If y is leap year, then x also, if y is normal year, then x also normal year.
The idea is to check all years one by one from next year. We will keep track of number of days moved ahead. If there are total 7 moved days, then current year begins with same day. We also check if the current year is leap year or not, if so, then also check for y. If both conditions are satisfied, we return current year.
Example
#include<iostream> using namespace std; int countExtraDays(int y) { if (y%400==0 || y%100!=0 && y%4==0) return 2; return 1; } int nextIdenticalYear(int y) { int days = countExtraDays(y); int x = y + 1; for (int sum=0; ; x++) { sum = (sum + countExtraDays(x)) % 7; if ( sum==0 && (countExtraDays(x) == days)) return x; } return x; } int main() { int curr = 2019; cout << "Next identical year of " << curr <<" is: " << nextIdenticalYear(curr); }
Output
Next identical year of 2019 is: 2030
- Related Articles
- Write the difference between calendar year and fiscal year.
- Print calendar for a given year in C++
- Display Month of Year using Java Calendar
- Java Program to subtract 1 year from the calendar
- Get week of month and year using Java Calendar
- Java Program to Display Dates of Calendar Year in Different Format
- Why 2023 is the year of next-level cloud value?
- Calendar Functions in Python - ( calendar(), month(), isleap()?)
- Combine three strings (day, month, year) and calculate next date PHP?
- The calendar Module in Python
- Find next Smaller of next Greater in an array in C++
- Calendar Functions in Python | Set 1( calendar(), month(), isleap()…)
- C++ Program to find joining year from the course year lists
- Calendar in python
- Find next palindrome prime in C++

Advertisements