Q. Write a program to check whether the given year is leap year or not.



Leap year :- A leap year is a calendar year that contains an additional day added to keep the calendar year synchronized. A leap year is exactly divisible by 4, except for the century years (the years ending with 00). Century year is a leap year only if it is completely divisible by 400.

For Example:-
Input = 1999
Output = 1999 is not a leap year
So as we can see that 1999 is not a leap year because 1999 is not divisible by 4.

Leap Year Algorithm

START
Step 1 → Initialize variable year
Step 3 → Check if year is divisible by 4 but not by 100, DISPLAY "leap year"
Step 4 → Check if year is divisible by 400, DISPLAY "leap year"
Step 5 → Otherwise, DISPLAY "not leap year"
STOP


Leap Year Program

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • #include <stdio.h>
    int main() {
    int check_year=1998;
    if (check_year % 4 == 0) {
    if (check_year % 100 == 0) {
    // the check_year is a leap year if it is divisible by 400.
    if (check_year % 400 == 0)
    printf("%d is a leap year.", check_year);
    else
    printf("%d is not a leap year.", check_year);
    } else
    printf("%d is a leap year.", check_year);
    } else
    printf("%d is not a leap year.", check_year);
    return 0;
    }
    
    #include <bits/stdc++.h>
    using namespace std; 
    int main()
    {
    int check_year=1998;
    if (check_year % 4 == 0)
    {
    if (check_year % 100 == 0)
    {
    if (check_year % 400 == 0)
    cout << check_year << " is a leap year.";
    else
    cout << check_year << " is not a leap year.";
    }
    else
    cout << check_year << " is a leap year.";
    }
    else
    cout << check_year << " is not a leap year.";
    return 0;
    }
    
    public class Main {
    public static void main(String[] args) {
    int check_year = 1998;
    boolean leap = false;
    if(check_year % 4 == 0)
    {
    if( check_year % 100 == 0)
    {
    // check_year is divisible by 400, hence the check_year is a leap year
    if ( check_year % 400 == 0)
    leap = true;
    else
    leap = false;
    }
    else
    leap = true;
    }
    else
    leap = false;
    if(leap)
    System.out.println(check_year + " is a leap year.");
    else
    System.out.println(check_year + " is not a leap year.");
    }
    }
    
    # Python program to check if year is a leap year or not
    check_year = 1998
    # To get year (integer input) from the user
    # check_year = int(input("Enter a year: "))
    if (check_year % 4) == 0:
    if (check_year % 100) == 0:
    if (check_year % 400) == 0:
    print("{0} is a leap year".format(check_year))
    else:
    print("{0} is not a leap year".format(check_year))
    else:
    print("{0} is a leap year".format(check_year))
    else:
    print("{0} is not a leap year".format(check_year)) 
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace Program
    {
    class leapyear
    {
    static void Main(string[] args)
    {
    leapyear obj = new leapyear();
    obj.readdata();
    obj.leap();
    }
    int check_year;
    public void readdata()
    {
    check_year = 1998;
    }
    public void leap()
    {
    if ((check_year % 4 == 0 && check_year % 100 != 0) || (check_year % 400 == 0))
    {
    Console.WriteLine("{0} is a Leap Year", check_year);
    }
    else
    {
    Console.WriteLine("{0} is not a Leap Year", check_year);
    }
    Console.ReadLine();
    }
    }
    }
    
    function isLeap($check_year)  
    {  
    return (date('L', mktime(0, 0, 0, 1, 1, $check_year))==1);  
    }  
    //For testing  
    $check_year=1998;
    If (isLeap($check_year))  
    {  
    echo "$check_year is a Leap Year\n";  
    }  
    else  
    {  
    echo "$check_year is not a Leap Year\n";  
    }  
    
    #include <stdio.h>
    int main() {
    int check_year=1998;
    if (check_year % 4 == 0) {
    if (check_year % 100 == 0) {
    // the check_year is a leap year if it is divisible by 400.
    if (check_year % 400 == 0)
    printf("%d is a leap year.", check_year);
    else
    printf("%d is not a leap year.", check_year);
    } else
    printf("%d is a leap year.", check_year);
    } else
    printf("%d is not a leap year.", check_year);
    return 0;
    }
    

    Output

    1998 is not a leap year.
    

    Recommended Programs

       Binary search algorithm and program
       Bubble sort program (Ascending Order)