by Sai gowtham

C++ Program to check a Leap Year or not

In this example program, you will learn about how to check if a given year is a leap year or not in C++.

#include<iostream>
using namespace std;

int main() {

   int year;
   cout << "Enter a year in (yyyy) format: ";
   cin >> year;

   if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
   cout<<year<<" is a leap year";
   else
   cout<<year<<" is not a leap year";
   return 0;
}

Output:

Enter a year in (yyyy) format: 2020

2020 is a leap year

The logic in the above program is if a given year is divisible by 4 and 100 but not divisible by 400 it is a common year else the given year is a leap year.

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course