Spaces:
Running
Running
File size: 1,847 Bytes
de4f63d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | '''
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
''' |