beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

C++ Program to Check Leap Year using function

By Chaitanya Singh | Filed Under: C++ Programs

This program takes the value of year(entered by user) and checks whether the input year is Leap year or not. The steps to check leap year are as follows:
To determine whether a year is a leap year, here are the steps: Source of these steps.
Step 1: If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
Step 2: If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
Step 3: If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
Step 4: The year is a leap year (it has 366 days).
Step 5: The year is not a leap year (it has 365 days).
Lets write these steps in a C++ program.

Example: Program to check whether the input year is leap year or not

To understand this program, you should have the knowledge of C++ functions and if-else.

#include <iostream>
using namespace std;

bool leapYear(int y);

int main(){
   int y;
   cout<<"Enter year: ";
   cin>>y;
   //Calling function
   bool flag = leapYear(y);
   if(flag == true)  
      cout<<y<<" is a leap Year"; 
   else 
      cout<<y<<" is not a leap Year";
   return 0;
}
bool leapYear(int y){
   bool isLeapYear = false;
   if (y % 4 == 0) {
      if (y % 100 == 0) {
         if (y % 400 == 0) {
            isLeapYear = true;
         }
      } 
      else isLeapYear = true;
   }
   return isLeapYear;
}

Output:

Enter year: 2016
2016 is a leap Year

Enjoyed this post? Try these related posts

  1. C++ Program to Find and display the Transpose of a Matrix
  2. C++ Program to Add two Matrices
  3. C++ Program to Add two Times entered by User
  4. C++ Program to find the Missing Number
  5. C++ Program to find the sum of n natural numbers
  6. C++ Program to add two numbers

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2019 BeginnersBook . Privacy Policy . Sitemap