Tutorial Playlist
191 Lessons1. Introduction to Java
2. What is Java?
3. History of Java
4. Java Tutorial for Beginners
5. How Do Java Programs Work?
6. JDK in Java
7. C++ Vs Java
8. Java vs. Python
9. Java vs. JavaScript
10. From Java Source Code to Executable
11. How to Install Java in Linux
12. How to Install Java in Windows 10
13. Java Hello World Program
14. Structure of Java Program and Java Syntax
15. Operators in Java
16. Java If-else
17. Switch Case In Java
18. Loops in Java
19. Infinite loop in Java
20. For Loop in Java
21. For Each Loop in Java
22. Constructor in Java
23. Constructor Overloading in Java
24. Copy Constructor in Java
25. Default Constructor in Java
26. Parameterized Constructors in Java
27. Constructor Chaining In Java
28. Finalize Method in Java
29. Static Method in Java
30. Equals Method in Java
31. Abstract Method in Java
32. toString() Method in Java
33. Difference between equals method in Java
34. Inheritance in Java
35. Multiple Inheritance in Java
36. Hierarchical Inheritance in Java
37. Java Classes and Objects
38. Scanner Class in java
39. All classes in java are inherited from which class
40. What is Nested Class in Java
41. POJO Class in Java
42. Anonymous Class in Java
43. Final Class in Java
44. Object Class in Java
45. Packages in Java
46. Access Modifiers in Java
47. Static Keyword In Java
48. Final Keyword in Java
49. Checked and Unchecked Exceptions in Java
50. User Defined Exception in Java
51. Error vs. Exception in Java
52. Java Collection
53. Collections in Java
54. Garbage Collection in Java
55. Generics In Java
56. Java Interfaces
57. Functional Interface in Java
58. Marker Interface in Java
59. Streams in Java
60. Byte stream in java
61. File Handling in Java
62. Thread in Java
63. Thread Lifecycle In Java
64. Daemon Thread in Java
65. Thread Priority in Java
66. Deadlock in Java
67. String Pool in Java
68. Java Database Connectivity(JDBC)
69. Design Patterns in Java
70. Functional Programming in Java
71. OOP vs Functional vs Procedural
72. Heap Memory and Stack Memory in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
76. Hibernate Framework
77. JUnit Testing
78. How to Install Eclipse IDE for Java?
79. Command line arguments in Java
80. Jar file in Java
81. Java Clean Code
82. OOPs Concepts in Java
83. Java OOPs Concepts
84. Overloading vs Overriding in Java
85. Java 8 features
86. String in Java
87. String to int in Java
88. Why String Is Immutable in Java?
89. Primitive Data Types in Java
90. Non-Primitive Data Types in Java
91. This and Super Keyword in Java
92. HashMap in Java
93. Comparable And Comparator in Java
94. Type Casting in Java
95. Arrays Sort in Java with Examples
96. Variable Hiding and Variable Shadowing in Java
97. Enum in Java
98. Substring in Java
99. Pattern Programs in Java
100. Hashcode in Java
101. What is ByteCode in Java?
102. How To Take Input From User in Java
103. GCD of Two Numbers in Java
104. Linked List in Java
105. Arithmetic Operators in Java
106. Conditional Operators in Java
107. Stack and Queue in Java
108. Array Length in Java
109. Number Pattern Program in Java
110. Split in java
111. Map In Java
112. Difference Between Throw and Throws in Java
113. Difference Between Data Hiding and Abstraction
114. HashSet in Java
115. String Length in Java
116. Factorial Using Recursion in Java
117. DateFormat in Java
118. StringBuilder Class in java
119. Instance variables in Java
120. Java List Size
121. Java APIs
122. Reverse an Array in Java
123. StringBuffer and StringBuilder Difference in Java
124. Java Program to Add Two Numbers
125. String to Array in Java
126. Regular Expressions in Java
127. Identifiers in Java
128. Data Structures in Java
129. Set in Java
130. Pass By Value and Call By Reference in Java
131. Try Catch in Java
132. Bubble Sort in Java
133. Caesar Cipher Program in Java
134. Queue in Java
135. Object Creation in Java
136. Multidimensional Array in Java
137. How to Read a File in Java
138. String Comparison in Java
139. Volatile Keyword in Java
140. Control Statements in Java
141. Jagged Array in Java
142. Two-Dimensional Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
147. Matrix Multiplication in Java
148. Static Variable in Java
149. Event Handling in Java
150. parseInt in Java
151. Java ArrayList forEach
152. Abstraction in Java
153. String Input in Java
154. Logical Operators in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
159. Stringtokenizer in java
160. Implementing and Manipulating Abs in Java
161. Char array to string in java
162. Convert Double To String In Java
163. Deque in Java
164. Converting a List to an Array in Java
165. The Max function in java
166. Removing whitespace from string in java
167. String arrays in Java
168. Strings in Java Vs Strings in Cpp
169. Sum of digits of a number in Java
170. Art of Graphical User Interfaces
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
175. Difference Between Java and Python
176. Square Root in Java
177. Reverse A String in Java
178. Even Odd Program in Java
179. Fibonacci Series in Java
180. Prime Number Program in Java
181. Java Program to Print Prime Numbers in a Given Range
182. Java Leap Year Program
Now Reading
183. Swapping of Two Numbers in Java
184. LCM of Two Numbers in Java
185. Math.sqrt() Function in Java
186. Area of Triangle in Java
187. Sort a String In Java
188. Factorial Program in Java
189. Javafx
190. Lambda expression in java
191. Setup Java Home and IDE on macOS
The Java leap year program is a simple yet important exercise for beginner-level programmers. It involves applying conditional statements to determine whether a given year is a leap year.
Understanding leap year logic is crucial for building foundational programming skills involving conditionals, arithmetic operations, and logical operators.
This tutorial will cover the logic behind leap years and guide you through the step-by-step implementation of a Java leap year program.
A Java leap year program determines whether a given year is a leap year. A leap year is a year that can be divided by 4 evenly, except for years that can be divided by 100 but not by 400.
The program typically takes an input year, performs the necessary calculations or checks, and shows whether the year is a leap year. It is a basic example of using conditional statements and arithmetic operations in Java to solve a specific problem related to date and time.
The if-else statements allow us to evaluate multiple conditions and execute the appropriate code block based on the results. This approach provides a clear and straightforward way to determine leap years.
public class upGradTutorials {
public static void main(String[] args) {
int year = 2024;
// Checking if the year is a leap year using if-else statements
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
System.out.println(year + " is a leap year");
} else {
System.out.println(year + " is not a leap year");
}
} else {
System.out.println(year + " is a leap year");
}
} else {
System.out.println(year + " is not a leap year");
}
}
}
In this example, we use a series of nested if-else statements to determine whether a given year is a leap year. First, we declare a variable year and assign it the value 2024.
Then, we check the conditions for a leap year using if-else statements:
Finally, we use System.out.println() statements to display the result, stating whether the year is a leap year.
The ternary operator provides a concise way to assign a value based on a condition conditionally. It simplifies the code and makes it more readable by avoiding nested if-else statements.
Like the previous example, we first declare a variable year and assign it the value 2024 in this example.
Then, we use the ternary operator to check the conditions for a leap year:
The result of the ternary operator is stored in the string variable leapYearStatus, which will be assigned either "Leap Year" or "Not a Leap Year" based on the conditions. Finally, we use a System.out.println() statement to display the result, stating whether the year is a leap year.
Using methods lets us encapsulate the logic for determining leap years into a reusable and modular code. This promotes code reusability and maintainability.
We will use the same logic for determining leap years as the first method (first example with if-else Statements). The difference lies in how the code is structured and expressed, offering different styles and levels of abstraction.
public class upGradTutorials {
public static void main(String[] args) {
int year = 2024;
// Checking if the year is a leap year using a method
boolean isLeapYear = isLeapYear(year);
// Displaying the result
System.out.println(year + " is " + (isLeapYear ? "a leap year" : "not a leap year"));
}
public static boolean isLeapYear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true;
} else {
return false;
}
} else {
return true;
}
} else {
return false;
}
}
}
In the above program example, we define a method isLeapYear() that takes an integer parameter year and returns a boolean value indicating whether the year is a leap year or not.
Inside the isLeapYear() method, we use a series of nested if-else statements to check the conditions for a leap year, similar to Method 1. If the conditions are satisfied, we return true; otherwise, we return false.
In the main() method, we declare a variable year and assign it the value 2024. We then call the isLeapYear() method, passing the year as an argument. The result is stored in the boolean variable isLeapYear. Finally, we use a System.out.println() statement to display the result, stating whether the year is a leap year.
Using the command line input allows for dynamic checking of leap years based on user input. It provides flexibility and interactivity to the program.
import java.util.Scanner;
public class upGradTutorials {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = scanner.nextInt();
scanner.close();
// Checking if the year is a leap year using if-else statements
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
System.out.println(year + " is a leap year");
} else {
System.out.println(year + " is not a leap year");
}
} else {
System.out.println(year + " is a leap year");
}
} else {
System.out.println(year + " is not a leap year");
}
}
}
In this program, we use the command line to input a year and check if it is a leap year.
We use the Scanner class to read input from the command line. We prompt the user to enter a year and store it in the variable year.
Then, we use a series of nested if-else statements to determine if the year is a leap year, as explained in Method 1. Finally, we use System.out.println() statements to display the result, stating whether the year is a leap year.
In this program, a fixed year value of 2024 is assigned to the variable year. The program uses a series of nested if-else statements to determine if the year is a leap year.
This program uses the Scanner class to take user input for the year. The user is prompted to enter a year using the System.out.print() statement.
We create a Scanner object named scanner to read the input from the console. We use scanner.nextInt() to read an integer value entered by the user and assign it to the variable year.
After reading the input, we call scanner.close() to release the resources associated with the Scanner object. This leap year program in Java using scanner follows the same logic as the previous program to determine if the entered year is a leap year.
import java.time.Year;
public class upGradTutorials {
public static void main(String[] args) {
int year = 2024;
// Using in-built isLeap() method to check if the year is a leap year
boolean isLeapYear = Year.of(year).isLeap();
// Displaying the result
if (isLeapYear) {
System.out.println(year + " is a leap year");
} else {
System.out.println(year + " is not a leap year");
}
}
}
This program uses the in-built isLeap() method from the java.time.Year class to check if the year is a leap year. We pass the year value (2024) to the Year.of() method, which returns a Year object. Then, we call the isLeap() method on the Year object to get a boolean value indicating if the year is a leap year.
Based on the result, we use an if-else statement to print the appropriate message stating whether the year is a leap year. Note: The isLeap() method internally follows the same logic described in the earlier methods to determine if the year is a leap year.
To verify the correctness of a Java Leap Year program, you can follow these rules:
By applying these rules, you can ensure that the program correctly identifies leap years.
To further verify the program, you can test it with various inputs, including leap and non-leap years, and compare the output against the expected results. Additionally, consider edge cases such as negative years or years beyond the range of valid years to ensure the program handles them gracefully.
public class upGradTutorials {
public static void main(String[] args) {
int startYear = 2000;
int endYear = 2023;
System.out.println("Leap years between " + startYear + " and " + endYear + ":");
for (int year = startYear; year <= endYear; year++) {
if (isLeapYear(year)) {
System.out.println(year);
}
}
}
public static boolean isLeapYear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true;
} else {
return false;
}
} else {
return true;
}
} else {
return false;
}
}
}
In this leap year program in Java using for loop, we find the leap years within a given range specified by the variables startYear and endYear. We assume the range is from 2000 to 2023.
We use a for loop to iterate through each year within the range. We call the isLeapYear() method for each year to check if it is a leap year. The isLeapYear() method follows the same logic as explained in the previous examples to determine whether a year is a leap year. This leap year program in Java using if else returns true if the year is a leap year and false otherwise.
If a leap year is within the range, we print it using the System.out.println() statement.
public class upGradTutorials {
public static void main(String[] args) {
String startYearString = "2000";
String endYearString = "2023";
int startYear = Integer.parseInt(startYearString);
int endYear = Integer.parseInt(endYearString);
System.out.println("Leap years between " + startYear + " and " + endYear + ":");
for (int year = startYear; year <= endYear; year++) {
if (isLeapYear(year)) {
System.out.println(year);
}
}
}
public static boolean isLeapYear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true;
} else {
return false;
}
} else {
return true;
}
} else {
return false;
}
}
}
Like the Java leap year program (for loop technique), we use two string variables in this program, startYearString and endYearString while assuming the range is from "2000" to "2023".
We convert the string values to integers using the Integer.parseInt() method and assign them to the variables startYear and endYear.
The rest of the program follows the logic explained in the previous example. We iterate through each year within the range, call the isLeapYear() method to check if it is a leap year, and print the leap years using the System.out.println() statement.
This tutorial explored the Java leap year program, delving into the underlying logic and its significance for beginner-level programmers. Armed with this knowledge, can handle similar programming challenges confidently and further expand your skills in Java. You can also consider taking up a comprehensive technical course offered by upGrad to hone your programming skills.
1. Can we write a leap year program in JavaScript?
Yes, we can write a JavaScript program to determine whether a given year is a leap year. We can use conditional statements and the modulo operator (%) to check for divisibility.
2. What is a century leap year program in Java?
A century leap year program in Java is a program that determines if a given year, typically a multiple of 100, is a leap year according to the leap year rules.
3. How can you find the leap year in array Java?
To find the leap year in an array in Java, iterate through the array elements and apply the leap year logic to check whether each year is a leap year.
PAVAN VADAPALLI
Director of Engineering
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...