Home » C++ programs » C++ Most popular & searched programs

C++ program to check leap year

In this program, we will learn how to check Leap year? Here we are taking year from the user and checking whether input year is Leap year or not.

Logic
A year is Leap year, if it satisfies two conditions:

  1. Year is divisible by 400 (for century years)
  2. Year is divisible by 4 and not divisible by 100 (for non century years)

Program to check Leap year or not in C++



#include <iostream>
using namespace std;

int main()
{
	int year;
	
	//read year
	cout<<"Enter a year: ";
	cin>>year;
	
	if((year%400==0)||(year%4==0 && year%100!=0))
		cout<<year<<" is Leap year"<<endl;
	else
		cout<<year<<" is not Leap year"<<endl;
	
	return 0;
}

Output

First run:
Enter a year: 2000
2000 is Leap year 

Second run:
Enter a year: 2005
2005 is not Leap year 

Third run:
Enter a year: 1900
1900 is not Leap year 

Fourth run:
Enter a year: 1904
1904 is Leap year 





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.