KaiquanMah's picture
boolean div4 = ((yr%4)==0);
de4f63d verified
raw
history blame
1.85 kB
'''
Write a program that asks the user for the year and indicates whether the given year is a leap year or not.
A year is a leap year if it is evenly divisible by four.
However, a year divided by 100 is a leap year only if it is also divided by 400.
So, for example, 2000 and 2004 are leap years, but 1900 is not.
Example output 1:
Give a year: 1984
Is a leap year
Example 2:
Give a year: 1983
Not a leap year
'''
import java.util.Random;
import java.util.Scanner;
public class Test{
public static void main(String[] args){
final Random r = new Random();
Scanner reader = new Scanner(System.in);
System.out.print("Give a year: ");
int yr = Integer.valueOf(reader.nextLine());
boolean div4 = ((yr%4)==0);
boolean div100 = ((yr%100)==0);
boolean div400 = ((yr%400)==0);
if (div100) {
if (div400) {
System.out.println("Is a leap year");
}
else {
System.out.println("Not a leap year");
}
}
else if (div4) {
System.out.println("Is a leap year");
}
else {
System.out.println("Not a leap year");
}
}
}
'''Testing with input 1984
Give a year: 1984
Is a leap year
Testing with input 1983
Give a year: 1983
Not a leap year
Testing with input 1900
Give a year: 1900
Not a leap year
Testing with input 2000
Give a year: 2000
Is a leap year
Testing with input 2002
Give a year: 2002
Not a leap year
Testing with input 2004
Give a year: 2004
Is a leap year
Testing with input 2020
Give a year: 2020
Is a leap year
Testing with input 2018
Give a year: 2018
Not a leap year
Testing with input 1800
Give a year: 1800
Not a leap year
Testing with input 1700
Give a year: 1700
Not a leap year
'''