Updated March 23, 2023
Introduction to Leap Year Program in C++
A year is called a leap year if it was 366 days instead of 365. Leap year has one additional day that is added to the calendar. We can also say that a year which is divisible by 4 is called a leap year. However, years that are divisible by 400 are also called as a leap year, but the years that are divisible by 100 do not leap years. calculating leap year theoretically and mathematically is quite tedious but the programming languages make it easy. We don’t have to worry about the mathematical calculation. Writing one simple program can reduce our work, we can check any year. So here in this article, we are going to see how to write a leap year program in C++ language
Examples to Implement Leap Year in C++
Examples to show the implementation of the leap year are given below.
Example #1
Leap year program in C++ using nested if Statement.
Code:
#include <iostream>
using namespace std;
int main()
{
int y = 2400;
if (y % 4 == 0)
{
if (y % 100 == 0)
{
if (y % 400 == 0)
cout << "Entered year is a leap year.";
else
cout << "Entered year is not a leap year.";
}
else
cout << "Entered year is a leap year.";
}
else
cout << "Entered year is not a leap year.";
return 0;
}
Code Explanation:
Here we have written a program to check the leap year using the nested if Statement. Here we have used a fixed value. here we have the Initialized variable y to 2400. that our year is 2400. First, it checks if the year is divisible by 4 or not. Then it checks if the year is divisible by 100 or not. If it is divisible by 100, it checks if it is divisible 400. If it is divisible by 400 also, then it will print Entered number is a leap year or else it will print Entered number is not a leap year. If the year is not divisible by 100, then it will print Entered number is a leap year. Here is the output for the year 2400.
Output:
If we want to check for the year 1700 we just need to change the value of y. Here we have changed the value of y to 1700.
int y = 1700;
Output for the year 1700 is:
Example #2
Program to check leap year in C++ using nested if Statement and a random value.
Code:
#include<iostream>
using namespace std;
int main()
{
int y;
cout << "Enter a year: ";
cin >> y;
if (y % 4 == 0)
{
if (y % 100 == 0)
{
if (y % 400 == 0)
cout << "Entered year is a leap year.";
else
cout << "Entered year is not a leap year.";
}
else
cout << "Entered year is a leap year.";
}
else
cout << "Entered year is not a leap year.";
return 0;
}
Code Explanation:
Here we have written a program that allows the user to enter the years and check whether the year is a leap year or not.
Output:
Example #3
Program to check leap year in C++ using if else statement.
#include <iostream>
using namespace std;
int main()
{
int y = 1600;
if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
cout << "Entered year is a leap year";
else
cout<<"Entered year is not a leap year";
return 0;
}
Code Explanation:
Here we have written a program to check the leap year using the if else statement. Here we have used fixed value here we have Initialized variable y to 1600.that our year is 1600. First, it checks if the year is divisible by 4 or not. Then it checks if the year is divisible by 100 or not. If it is divisible by 100, it checks if it is divisible 400. If it is divisible by 400 also, then it will print Entered number is a leap year or else it will print Entered number is not a leap year. Here we make use of && operator and || operator to apply the condition and reduce the number coding statement.
Output:
If we want to check for the year 1800 we just need to change the value of y. Here we have changed the value of y to 1800.
int y = 1800;
Output for the year 1800 is:
Example #4
Program to check leap year in C++ using if else Statement and a random value.
#include <iostream>
using namespace std;
int main()
{
int y;
cout << "Enter a year: ";
cin >> y;
if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
cout << "Entered year is a leap year";
else
cout<<"Entered year is not a leap year";
return 0;
}
Code Explanation:
Here we have written a program that allows users to enter the years and check whether the year is a leap year or not.
Output:
Recommended Articles
This is a guide to Leap Year Program in C++. Here we discuss the Introduction and examples to implement leap year in C++ with its code explanation. You may also have a look at the following articles to learn more –