×

Leap Year Program in C++

What is a Leap Year?

A solar year is the length of time that it takes for Earth to orbit the Sun - approximately 365.25 days. In a calendar year, we usually round the number of days to 365 as a standard. We add a day to our calendar in every four years to make up for the missing, partial day, and that year is a leap year.

How to find a leap year using Programming?

Generally, if any year is divisible by 4, it is a leap year. But a few years do not leap years and are divisible by 4. Generally, those years will end with zeros (Centurian years). Example; The year 2100 is the year which will be divisible by 4, but it is not a leap year similarly.

  • So first, we need to check whether the entered year is a Centurian year.
  • If it is a Centurian year, we need to check if it is divisible by 400.
  • If it is divisible by 400, then it is a leap year.
  • Then we need to write another statement to check if the entered year is divisible by 4 but not divisible by 100. If yes, then it is a leap year. Else it is not a leap year.

First, we will write a code to check if the year is divisible by 4 or not:

Note: This is not the final code

//The below code is written to check if the year entered is a leap year or not
//Please note that this is not the final code.
#include <iostream>


using namespace std;


// create the main function
int main(){
  int a;
  cout<<"Enter any year:"<<endl;
  //Taking user input.
  cin>>a;
  //If it is divisible by 4, it is a leap year.
  if(a%4==0){
    cout<<"The entered year is a leap year"<<endl;
  }
  else{
    cout<<"The entered year is not a leap year"<<endl;
  }


}

Output:

Leap Year Program In C++ Leap Year Program In C++

Explanation:

The above code is written to check whether the year is a leap year. We have taken input from the user using the cin statement in the code. Using the if statement, we have checked if the year is divisible by 4. If the year is divisible by 4, then the output will be the year is a leap year. Else the year will not be a leap year. In output1, we can see that 2020 is a leap year as 2020 will be divisible by 4, whereas in output2, we can see that the year 2100 is stated as a leap year which is not a leap year. So before checking if the year is divisible by 4, we need to check if the year is a Centurian year. If it is a Centurian year, then we need to check if it is divisible by 400.

Final code

//The below code is written to check if the year entered is a leap year or not
#include<iostream>
using namespace std;
int main() {
   int year;
   //Taking input from the user.
   cout<<"Enter any year: "<<endl;
   cin>>year;
   //The first condition is to check if the year is a Centurian year.
   if(year%100==0){
     //If the year is a centurion year, then we need to check
     //if it is divisible by 400 or not if divisible then it is a leap year
     if(year%400==0){
       cout<<"The entered year is a leap year"<<endl;
     }
     else{
       cout<<"The entered year is not a leap year"<<endl;
     }
   }
   //If the year is not Centurian, this block will execute.
   else{
     //We need to check if the non-centurion year is divisible by 4 or not
     //If it is divisible by 4, the output will be a leap year.
     if(year%4==0){
       cout<<"The entered year is a leap year"<<endl;
     }
     else{
       cout<<"The entered year is not a leap year"<<endl;
     }
   }
}

Output:

Leap Year Program In C++

Explanation:

The above code is written to check whether the year is a leap year. We have taken the year input from the user using the cin statement in the main function. Then we need to check whether the year is a Centurian. We need to check if the year is divisible by 100 or not. If the year is divisible by 100, then we need to check whether the year is divisible by 400. If it is divisible by 400, then the centurion year is a leap year. If not, then the year is not a leap year. If the entered year is not a Centurian year, then we need to check if the year is divisible by 4. If it is divisible by 4, then it is a leap year. If not divisible by 4, then it is not a leap year.


Related Topics

C++ Continue in for loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

4 minutes read.

C++ Maximum Index Problem

Given an array A[] of positive integers. We will find the maximum of (j-i) such that i and j are the indexes of A[] and A[i] <= A[j], i<=j For...

5 minutes read.

Reverse a Number in C++

In this tutorial, you'll learn how to utilize a C++ program to reverse a number entered by a user at runtime. Because there are various ways to write a C++...

3 minutes read.

Continue in C++ While loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the...

4 minutes read.

Message Passing in C++

The act of sending and receiving information by an object is referred to as communication, and all communication between objects that takes place via message is known as message passing....

1 minute read.

Use of Inheritance in C++

What is inheritance in c++? Inheritance is fundamental in object-oriented programming (OOP) languages like C++. It allows a programmer to create a new class (called a derived class) that inherits the...

4 minutes read.

Hexadecimal to Decimal in C++

In computers, hexadecimal numbers are represented with base 16 and decimal numbers are represented with base 10 and values 0-9, whereas hexadecimal numbers have digits ranging from 0 to 15,...

3 minutes read.

Stack in C++

Stack: The stack is a very popular data structure. It is the form of data structure that follows a particular order called FIFO(First-In-First-Out). In simple words, a stack is an Abstract...

4 minutes read.

How the value is passed in C++

Introduction: The call-by-value method of giving arguments to a function duplicates the real value of an argument into the formal parameter of the function. In this instance, modifications to the parameter...

5 minutes read.

How to implement map in C++

Part of the C++ STL is maps (Standard Template Library). Maps are associative containers that hold sorted key-value pairs, where each key is distinct and may only be added or...

4 minutes read.

C++ array to function

Arrays in C++ : Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. An array can be declared by specifying the variable...

4 minutes read.

Factorial Program in C++

C++ Factorial Program: The product of all positive descending integers is the factorial of n. n! denotes the factorial of n. For instance: 5! = 5*4*3*2*1=120 4! = 4*3*2*1=24 In Combinations and Permutations, the...

4 minutes read.

C++ Keywords

In this article, we will discuss keywords in C++ with their several features and functions. What are Keywords in C++? In C++, a keyword is a reserved word that has a predefined...

4 minutes read.

Dynamic Memory Allocation in C++

In some programming situations, the number of data items changes as the program is running, which is known as dynamic data or input. Consider a real-world situation where a program...

3 minutes read.

Division in C++

C++ Division Arithmetic Operation In C++ the arithmetic operator / is used for division. This operator takes two operands and returns the result of dividing the left operand by the right...

3 minutes read.

Computing index using Pointers Returned by STL Functions in C++

In this tutorial we will learn how to compute index using pointers which returned by STL functions in C++. Many built-in C++ functions return pointers to memory places that provide...

2 minutes read.

C++ If

C++ Control Statement C++ control statement or decision-making statement is used to control the flow of program statement according to condition applied. C++ if Control Statement An if control statement in C++ is used to...

2 minutes read.

Difference between "int main( ) and int main(void)" in C/C++

int main( ) function In the C/C++ programming language, int main() indicates a function that returns an integer at the end of the program execution. In general, a value of '0' indicates...

3 minutes read.

Is it fine to write void main() or main() in C/C++?

In C programming language: The default function return type in the C programming language is "int," which implies that main() will always return an integer value. In C, the void main()...

3 minutes read.

C++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.