🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Determining whether a year is a leap year is a common task in programming. A leap year occurs every 4 years to add an extra day to the calendar year, helping to keep our calendar in alignment with the Earth's revolutions around the Sun. However, simply being divisible by 4 does not guarantee that a year is a leap year. This blog post explains how to implement a Java program to check if a given year is a leap year, considering all the rules.
2. Program Steps
1. Import the Scanner class to read the year input from the user.
2. Prompt the user to enter a year.
3. Apply the leap year conditions to determine if the year is a leap year.
4. Display the result.
5. Close the Scanner object to prevent resource leaks.
3. Code Program
import java.util.Scanner;
public class LeapYearChecker {
public static void main(String[] args) {
// Create a Scanner object for reading input
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a year:");
int year = scanner.nextInt(); // Read the year entered by the user
// Closing the scanner
scanner.close();
// Leap year conditions
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
}
Output:
Enter a year: 2000 2000 is a leap year.
Explanation:
1. The program starts by importing the Scanner class for reading user input.
2. It prompts the user to enter a year, which is read from the console and stored in the year variable.
3. To determine if the year is a leap year, the program applies the following conditions: A year is a leap year if it is divisible by 4 but not by 100, except if it is also divisible by 400. This is implemented using an if-else statement that checks these conditions.
4. Depending on the outcome of the condition check, the program prints a message indicating whether the entered year is a leap year or not.
5. Lastly, the Scanner object is closed to avoid resource leaks. This program effectively demonstrates the use of conditional statements in Java to solve a problem based on specific rules.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment