Google Add

Search

C++ Program to Check Leap Year - Whether a Entered Year is Leap Year or Not

Write a c++ program to check whether a entered year is leap year or not. In this programming tutorial, We are going to write a c++ code which takes an input year and check whether a entered year is leap year or not.

In my previous tutorial, I have explained what is a leap year and algorithm to check leap year.

C program to check leap year

Programming videos

C++ Program to Check Leap Year



C++ Program to Check Leap Year using Function


A Leap year is a year which has 366 days. It means it has one additional day. How do we check whether a entered year is a leap year or not.

 i) If a year is divisible by 4 and not divisible by 100 then it's a leap year. For example - 2012, 2008 etc.

 ii) If a century year (1200, 1600) is divisible by 400 then it's a leap year. For example - 1200, 1600 etc.


C++ program to print factorial of a number using recursion


#include <iostream>

using namespace std;

bool checkLeapYear(int year) {
    
    /* Leap year is divisible by 4 but not by 100
         or it is divisible by 400
    */
      
    if( ( (year%4 == 0) && (year%100!=0) ) 
            || (year%400==0) ) 
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main(void) {
 
      int year;
   
      //Input year
      cout << "Enter a year\n";
      cin  >> year;
      
      
      //Method call to check leap year
      if( checkLeapYear(year) ) 
      {
           cout << year << " is a leap year.";
      }
      else
      {
           cout << year << " is not a leap year.";
      }

     return 0;     
}


Output : 

Enter a year 2008

2008 is a leap year

C interview questions with solutions

Sorting algorithm and their time complexities

Programming questions on arrays




No comments:

Post a Comment