Codeforcoding

Program to check if the given year is leap year in C++

Program to check if the given year is leap year

In this article, we will discuss the concept of Program to check if the given year is leap year

In this post, we are going to learn how to write a program to check and display  the given year is leap or not in  C++ programming language.

Program to check if the given year leap is leap year in C++
leap year

What is leap year

leap year is occurring once every four years, which has 366 days including 29 February as an intercalary day. it is called leap year

Ex – 2000,2004.2008

 

Century year

The century year is ending with 00

Ex 2000, 2100, 2200

a century year may be a leap year(only if the year is exactly divisible by 400)

 

How to find the leap year

if The given year is perfectly divisible by 4 , it is a leap year(except century year)

only if it is perfectly divisible by 400, the century year is a leap year,
a century year should be divisible by 4 and 100 both
a non century year should be divisible only by 4

Check if the given year is leap year using if else  #1

In this program, The program allows the user to enter the year and then it will check the given year is a leap year or not, using if – else statements in C++ language

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int year;//variable declaration

    cout<<"Enter a year for check leap or not\n";
    //ask input from the user
    cin>>year;
//store the input in the year variable
   if ((year%400==0)||((year%4==0)&&(year%100!=0))){
     cout<<year<<" is a leap year ";
}//display leap year
       else{
       cout<<year<<" is not a leap year ";
}
getch();
    return 0;
}

When the above code is executed, it produces the following result

Case 1

Enter a year for check leap or not
2052
2052 is a leap year

 

Case 2

Enter a year for check leap or not
2002
2002 is not a leap year

 

 

Check if the given year is leap year using if else if    #2

In this program, The program allows the user to enter the year and then it will check the given year is a leap year or not, using if – else if statements in C++ language

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int year;//variable declaration
    cout<<"Enter a year for check leap or not\n";
    //Ask input from the user for year
    cin>>year;
    //store the value on the year variable
    if(year%400==0){
            //if it is divisible by 400
     cout<<year<<" is a leap year ";
}
else if(year%100==0){
        //if it is divisible by 100
    cout<<year<<" is not a leap year ";
}

   else if(year%4==0){
       //if it is divisible by 4
       cout<<year<<" is a leap year ";
   }
       else{
       cout<<year<<" is not a leap year ";
}
getch();
    return 0;
}

When the above code is executed, it produces the following result

Case 1

Enter a year for check leap or not
2000
2000 is a leap year

 

Case 2

Enter a year for check leap or not
2006
2006 is not a leap year

 

 

Check if the given year is leap year using Nested  if

In this program, The program allows the user to enter the year and then it will check the given year is a leap year or not, using Nested if  statements in C++ language

Program 3

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int year;//variable declaration

    cout<<"Enter a year for check leap or not\n";
    //Ask input from the user
    cin>>year;
//store the input in the store variable
   if(year%4==0){
     if(year%100==0){
           if(year%400==0){
      cout<<year<<" is a leap year ";
        }else
     cout<<year<<" is not a leap year ";
    } else
     cout<<year<<" is a leap year ";
  }else
        cout<<year<<" is not a leap year ";
getch();
    return 0;
}

When the above code is executed, it produces the following result

Case 1

Enter a year for check leap or not
2240
2240 is a leap year

 

Case 2

Enter a year for check leap or not
2014
2014 is not a leap year

 

Check if the given year is leap year using ternary operator

Here, The program allows the user to enter the year and then it will check the given year is a leap year or not, using ternary operators in C++ language

Program 4

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int year;//variable declaration
    cout<<"Please Enter year for check leap: \n";
    //Ask input from the user
   cin>>year;//store the input in the variable year
    (year%4==0 && year%100!=0)? cout<<"The entered year is a leap year":year%400==0?
    cout<<"The entered year is a leap year"
:cout<<"This is not a leap year";
getch();
    return 0;
}

When the above code is executed, it produces the following result

Case 1

Please Enter  year for check leap or not
2024
The entered year is a leap year

 

Case 2

Please Enter  year for check leap or not
2030
The entered year is not a leap year

 

Suggested post

if statements in C++ language

Nested if statements in C++ language

Data type in C++ language

Variable in C++ language

Operator in C++ language

 

Similar post

Java program to find leap year

C program to find leap year

C++ program to find leap year

Python program to find leap year

 

C++ program for check whether given year is leap using function

Python program for check whether given year is leap using function

C program for check whether given year is leap using function

 

 

Program to check leap year in C language
Python Program to check leap year
Exit mobile version