- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to find if the given number is a leap year?
Finding a year is a leap or not is a bit tricky. We generally assume that if a year number is evenly divisible by 4 is a leap year. But it is not the only case. A year is a leap year if −
- 1. It is evenly divisible by 100
- 2. If it is divisible by 100, then it should also be divisible by 400
- 3. Except this, all other years evenly divisible by 4 are leap years.
Algorithm
- 1. Take integer variable year
- 2. Assign a value to the variable
- 3. Check if the year is divisible by 4 but not 100, DISPLAY "leap year"
- 4. Check if the year is divisible by 400, DISPLAY "leap year"
- 5. Otherwise, DISPLAY "not leap year"
Example
import java.util.Scanner; public class LeapYear { public static void main(String[] args){ int year; System.out.println("Enter an Year :: "); Scanner sc = new Scanner(System.in); year = sc.nextInt(); if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0)) System.out.println("Specified year is a leap year"); else System.out.println("Specified year is not a leap year"); } }
Output 1
Enter an Year :: 2020 Specified year is a leap year
Output 2
Java Programming questions 31 Enter an Year :: 2017 Specified year is not a leap year
Advertisements
To Continue Learning Please Login
Login with Google