×

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++ Pipe Tutorial

A pipe is a mechanism for inter-process communication (IPC) in a Unix-like operating system. It allows two or more processes to communicate with each other by sending and receiving data...

3 minutes read.

Advanced C++ with Boost Library

The goal of the Boost Libraries is to be widely applicable and used in a variety of applications. For instance, they can in handy when working with huge numbers whose...

4 minutes read.

C++ Iterators

What are iterators ? Iterators are among the four foundations of the C++ Standard Template Library, also known as the STL. The memory address of the STL container classes is pointed...

15 minutes read.

C++ Ternary Operator

In this tutorial, we'll learn about the C++ ternary operator and how to utilise it to manage the program's flow using examples. Ternary Operator: The if-else statement and the conditional operator use...

3 minutes read.

C++ Overriding

C++ Function Overriding When the base and derive class both contain the same function name and calling the function through derived object invokes derived class function called function overriding. Function overloading is...

1 minute read.

Handling multiple clients on the Server with multithreading using Socket Programming in C or C++

To understand this guide completely, the reader is assumed to be familiar with the foundations of server and client models and socket programming. If one wants to create any scalable...

7 minutes read.

Binary to Decimal in C++

We must create a software to convert a binary number into an equivalent decimal value given a binary number as input. Example: // C++ program to convert binary to decimal #include < iostream...

3 minutes read.

Iostream in C++

Using Iostream in C++, we can perform input and output operation capabilities. This represents input and output, and the stream is used to carry out this capability. A stream is...

4 minutes read.

Constructor Vs Destructor in C++

C++ Constructor A function Object () { [native code] } is a member function that shares the same name as the class. It is called automatically whenever a class object is...

3 minutes read.

gmtime() function in C/C++

C++ language is used to make high-performance applications that can work efficiently. It is one of the world's most popular languages. It is an object-oriented and high-level programming language, which...

4 minutes read.

C++ Multimap

Definition: In C++, a multimap is similar to a map with the additional concept, where multiple elements possess the same keys. It is also not necessary that the key values and...

4 minutes read.

Function overloading in C++

Function overloading in C++ As we know that C++ works on the OOP Concepts, that are abstraction, encapsulation, and data hiding, it also uses the other important feature of OOP, which...

8 minutes read.

Pure Virtual Function in C++ With Example Program

What is a Virtual Function? A virtual function is created inside a class with the keyword virtual. A virtual function does not have any value to be returned. Once a virtual...

3 minutes read.

External merge sort in C++

External merge sort in C++ External sorting is a concept for a group of sorting algorithms capable of handling large data volumes. External sorting is needed if the information getting sorted...

9 minutes read.

C++ Program For FCFS (First Come First Serve)

The most basic scheduling technique is FCFS, often known as "FIFO (First In, First Out)". In this procedure, the first one is utilized and executed first, while the second one...

4 minutes read.

Learn C++ Tutorial

C++ Introduction C++ is an object-oriented programming language. It was developed by Bjarne Stroustrup at AT&T Bell Laboratories. It is superset (extension) of C programming language. Depending upon features supported by programming...

10 minutes read.

How to Declare Unordered Sets in C++

The implementation of an unordered set using a hash table ensures that the insertion is always randomised by hashing the keys into hash table indices. When we define keys of...

4 minutes read.

Upcasting and Downcasting in C++

With the help of various examples in the C++ programming language, this section will cover Upcasting and Downcasting. Upcasting and downcasting, on the other hand, are two forms of object typecasting. Consider...

3 minutes read.

Floating Point Operations and Associativity in C, C++ and Java

In this tutorial, we are going to compare Floating-point operations and the concept of associativity. Before we apply the concept of associativity in the floating-point operations in all three programming...

3 minutes read.

C++ Close file

C++ File Handling close() Function A file which is opened while reading or writing in file handling must be closed after performing an action on it. The close() function is used to close...

2 minutes read.