Program to Check Whether the Year is a Leap or Not [C, C++, Python & Java]

Posted in

Program to Check Whether the Year is a Leap or Not [C, C++, Python & Java]
vinaykhatri

Vinay Khatri
Last updated on April 16, 2024

    This tutorial will help you learn to write a program in C, C++, Python, and Java to check whether a year is a leap or not.

    To write the program, you must know the logic to determine a leap year, the if...else statement, and the programming syntax.

    So, let us begin!

    What is a Leap Year?

    A leap year is a year that consists of an extra day in a year, i.e., 366 days. The extra day is added to the shortest month, i.e., February. So, whenever a leap year comes, the month of February has 29 days. It occurs after every four years.

    The major reason to add 1 extra day after every four years is to maintain the calendar year synchronized with the astronomical year or seasonal year.

    There are some conditions to be a leap year, as follows:

    • The year should be a multiple of 400.
    • The year should be a multiple of 4 and not 100.

    C Program to Check Whether the Year is a Leap or Not

    #include<stdio.h>
    int main()
    {
       int y;
       printf("Enter any year: ");
       scanf("%d", &y);
       
       if (y % 4 == 0)
       {
           if (y % 100 == 0)
           {
               if (y % 400 == 0)
               {
                   printf("It is a leap year");
               }
               else
               {
                   printf("It is not a leap year");
               }
           }
           else
           {
               printf("It is a leap year");
           }
       }
       else
       {
           printf("It is not a leap year");
       }
       
       return 0;
    }

    Output

    Enter any year: 4500
    It is not a leap year

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

    #include<iostream>
    using namespace std;
    
    int main()
    {
       int y;
       cout<<"Enter any year: ";
       cin>>y;
       
       if (y % 4 == 0)
       {
           if (y % 100 == 0)
           {
               if (y % 400 == 0)
               {
                   cout<<"It is a leap year";
               }
               else
               {
                   cout<<"It is not a leap year";
               }
           }
           else
           {
               cout<<"It is a leap year";
           }
       }
       else
       {
           cout<<"It is not a leap year";
       }
       
       return 0;
    }

    Output

    Enter any year: 5000
    It is a leap year

    Python Program to Check Whether the Year is a Leap or Not

    y= int(input("Enter any year: "))
    if y%4==0:
        if y%100:
            if y%400==0:
                print("It is a leap year")
            else:
                print("It is not a leap year")
        else:
            print("It is a leap year")
    else:
        print("It is not a leap year")

    Output

    Enter any year: 2019
    It is not a leap year

    Java Program to Check Whether the Year is a Leap or Not

    import java.util.Scanner;
    public class Main
    {
        public static void main(String[] args)
        {
            int y;
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter any year: ");
            y = sc.nextInt();
            if (y % 4 == 0)
            {
                if (y % 100 == 0)
                {
                    if (y % 400 == 0)
                    {
                        System.out.println("It is a leap year");
                    }
                    else
                    {
                        System.out.println("It is not a leap year");
                    }
                }
                else
                {
                    System.out.println("It is a leap year");
                }
            }
            else
            {
                System.out.println("It is not a leap year");
            }
        }
    }

    Output

    Enter any year: 
    2004
    It is a leap year

    Conclusion

    You might have got an idea of how to write a program in C, C++, Python, and Java to check whether a year is a leap or not. The only thing you need to take care of is the logic to determine the leap year. The correct use of if...else statements play a vital role here. Understand the flow of the program and implement it on your system.

    Good luck!

    People are also reading:

    Leave a Comment on this Post

    0 Comments