In the above C++ program to display all the leap years between two given years, we have used conditional statements in a user defined function. And in the main function the user-defined function is called along with the values of the year entered by the user.
#include <iostream>
#define range(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define rep(i,n) range(i,0,n)
using namespace std;
inline bool is_leap(int year){
if(year%400==0)
return true;
if(year%100==0)
return false;
if(year%4==0)
return true;
return false;
}
int main(void){
int a,b;
bool s=false;
cout<< "Enter two years to specify the range: ";
cin >> a >> b;
cout << "Input years: " << a << " - " << b;
cout << "\nLeap years between said years:\n";
if(s) puts("");
bool answer=false;
range(i,a,b+1) if(is_leap(i)) cout << i << endl,answer=true;
if(!answer) puts("No leap years between the entered years.");
s=true;
return 0;
}
Sample Output: Enter two years to specify the range: 2012 2023 Input years: 2012 - 2023 Leap years between said years: 2012 2016 2020