In this Java program, we will use Scanner class to get the input from a user, and then we are using if-else statements to write the logic to check leap year.
Steps
Let's write a java program to check whether the input year is a leap year or not. Before we see the program, let's see how to determine whether a year is a leap year mathematically: To determine whether a year is a leap year, follow these steps:
- If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
- If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
- If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
- The year is a leap year (it has 366 days).
- The year is not a leap year (it has 365 days).
Java Program to Check Leap Year
package com.javaguides.java.tutorial; import java.util.Scanner; /** * Java Program to check Leap Year * * @author https://www.sourcecodeexamples.net/ * */ public class JavaProgram { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { System.out.println("Enter any Year:"); int year = scanner.nextInt(); boolean isLeap = false; if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) isLeap = true; else isLeap = false; } else isLeap = true; } else { isLeap = false; } if (isLeap == true) { System.out.println(year + " is a Leap Year."); } else { System.out.println(year + " is not a Leap Year."); } } } }
Output:
Enter any Year: 2019 2019 is not a Leap Year.
Related Java Programs
- Java program to calculate the area of Triangle
- Java Program to Calculate Area of Square
- Java Program to Calculate Area of Rectangle
- Java Program to find the Smallest of three numbers using Ternary Operator
- Java Program to Find Largest of Three Numbers
- Java Program to Find GCD of Two Numbers
- Java Program to Check Armstrong Number
- Java Program to Generate Random Number
- Java Program to Check if Number is Positive or Negative
- Java program to check prime number
- Java Program to Calculate Simple Interest
- Java Program to Swap Two Numbers Without using a Temporary Variable
- Java Program to Swap Two Numbers
- Java Program to Find ASCII Value of a Character
- Java Program to Check Whether an Alphabet is Vowel or Consonant
- Java Program to Check Leap Year
- Java Program to Multiply Two Numbers
- Java Program to Check Even or Odd Number
- Java Program to Add Two Numbers
- Java Program to Swap Two Strings Without Using Third Variable
- Java Program to Swap Two Strings with Third Variable
- How to Get All Digits from String in Java
- Find Duplicate Number in Array in Java
- How to Get Current Working Directory in Java?
- Check Palindrome String in Java
- Java Program to Create Pyramid Of Numbers
Comments
Post a Comment