Leap Year Program in Java using If else Statement
ADVERTISEMENTS
Leap year program in java using if else statement. In this article, you will learn how to make a leap year program in java using if else statement.
Samples of the leap years
1996, 2000, ....., 2008, 2012 are the leap years
Leap Year Program in Java using If else Statement
// Leap Year Program in Java using If else Statement
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int y; // Value of the year
System.out.println ("----Enter the year----");
y = in.nextInt();
if (y % 400 == 0) {
System.out.println ("\n" + y+ " is the leap year.");
} else if (y % 100 == 0) {
System.out.println ("\n" + y + " is not the leap year.");
} else if (y % 4 == 0) {
System.out.println ("\n" + y + " is the leap year.");
} else {
System.out.println ("\n" + y + " is not the leap year.");
}
}
}
Output
----Enter the year----
2003
2003 is not the leap year.
Leap Year Program in Java using Switch Case Statement
// Leap Year Program in Java using Switch Case Statement
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int y, r;
// y - Value of the year
System.out.println ("----Enter the year----");
y = in.nextInt();
r = y % 400 == 0 || y % 100 == 0 || y % 4 == 0 ? 1 : 0;
switch (r) {
case 1:
System.out.println ("\n" + y+ " is the leap year.");
break;
case 0:
System.out.println ("\n" + y + " is not the leap year.");
break;
default:
System.out.println ("\n" + y + " is not the leap year.");
}
}
}
Output
----Enter the year----
2000
2000 is the leap year.