Coding Practice

Write a Java program to check whether a year is leap year or not

Check whether a year is leap year or not
Sample Output
Check whether a year is leap year or not.

Enter year: 1900
1900 is not leap year.
Java-Source Code
//package ifelsejava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int year;

        Scanner input = new Scanner(System.in);

        System.out.println("Check whether a year is leap year or not.\n");

        System.out.print("Enter year: ");
        year = input.nextInt();

        if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
            System.out.println(year + " is leap year.");

        } else {
            System.out.println(year + " is not leap year.");
            
        }
    }
}
Sample Output
Check whether a year is leap year or not.

Enter year: 2020
2020 is leap year.

No comments:

Post a Comment

Change Theme
X