codescracker
c++

C++ Program to Check Leap Year or Not



« Previous Program Next Program »

In this article, you will learn and get code on checking whether the given year by user at run-time is a leap year or not in C++ programming. The leap year program is created with following three approaches:

Before going through the program, if you are not aware about the logic used behind leap year, then I must recommed you to visit Leap Year Logic once. There, you will get everything about leap year. But in short, Here are the rules to check for leap year:

Check Leap Year in C++

To check whether the input year is a leap year or not in C++ programming, you have to ask from user to enter the year first and then apply the logic and print the message as shown in the program given below:

So the question is, write a program in C++ to check whether a year is a leap year or not. The answer to this question is given below:

// C++ Program to Check Leap Year or Not
// ----codescracker.com

#include<iostream>
using namespace std;
int main()
{
    int yr;
    cout<<"Enter the Year: ";
    cin>>yr;
    if((yr%4==0) && (yr%100!=0))
        cout<<"\nIt is a Leap Year";
    else if(yr%400==0)
        cout<<"\nIt is a Leap Year";
    else
        cout<<"\nIt is not a Leap Year";
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program check leap year

Now supply the input, that is enter any year to print whether it is a leap year or not as shown in the output given below with user input as 1900:

check leap year or not c++

Here is another sample run with user input as 2000:

leap year program in c++

Note - To be a leap year, if a year is divisible by 4, then it must not be divisible by 100.

Check Leap Year using Class

Now let's create the same purpose program using class and object. To learn about it, you can follow its separate tutorial.

// Check Leap Year or Not using Class
// ----codescracker.com

#include<iostream>
using namespace std;
class CodesCracker
{
    private:
        int yr;
    public:
        int getData();
        void checkLeapYear(int);
};
int CodesCracker::getData()
{
    cout<<"Enter the Year: ";
    cin>>yr;
    return yr;
}
void CodesCracker::checkLeapYear(int yr)
{
    if((yr%4==0) && (yr%100!=0))
        cout<<"\nIt is a Leap Year";
    else if(yr%400==0)
        cout<<"\nIt is a Leap Year";
    else
        cout<<"\nIt is not a Leap Year";
}
int main()
{
    CodesCracker c;
    int y;
    y = c.getData();
    c.checkLeapYear(y);
    cout<<endl;
    return 0;
}

This will produce the same output as of first program. Here is the very brief explanation of above program:

Leap Year Program using Function

This is the last program of this article. This program uses user-defined function, checkLeap() to check whether a year is a leap year or not.

// Check Leap Year using Function
// ----codescracker.com

#include<iostream>
using namespace std;
int checkLeap(int);
int main()
{
    int yr, val;
    cout<<"Enter the Year: ";
    cin>>yr;
    val = checkLeap(yr);
    if(val==1)
        cout<<"\nIt is a Leap Year";
    else
        cout<<"\nIt is not a Leap Year";
    cout<<endl;
    return 0;
}
int checkLeap(int yr)
{
    if((yr%4==0) && (yr%100!=0))
        return 1;
    else if(yr%400==0)
        return 1;
    else
        return 0;
}

You can also create the same purpose program in another way as given below. Rather returning the value according to the condition, function in this program just prints the message direct from inside the function without returning any value. You can also refer to function in C++ for its in-depth learning.

// ----codescracker.com

#include<iostream>
using namespace std;
void checkLeap(int);
int main()
{
    int yr, val;
    cout<<"Enter the Year: ";
    cin>>yr;
    checkLeap(yr);
    cout<<endl;
    return 0;
}
void checkLeap(int yr)
{
    if((yr%4==0) && (yr%100!=0))
        cout<<"\nIt is a Leap Year";
    else if(yr%400==0)
        cout<<"\nIt is a Leap Year";
    else
        cout<<"\nIt is not a Leap Year";
}

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker