KaiquanMah commited on
Commit
de4f63d
·
verified ·
1 Parent(s): 40efd14

boolean div4 = ((yr%4)==0);

Browse files
Week 1: Types, condition clauses and loops/11. Leap year? ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
+ Write a program that asks the user for the year and indicates whether the given year is a leap year or not.
3
+
4
+ A year is a leap year if it is evenly divisible by four.
5
+ However, a year divided by 100 is a leap year only if it is also divided by 400.
6
+
7
+
8
+ So, for example, 2000 and 2004 are leap years, but 1900 is not.
9
+
10
+ Example output 1:
11
+ Give a year: 1984
12
+ Is a leap year
13
+
14
+
15
+ Example 2:
16
+ Give a year: 1983
17
+ Not a leap year
18
+ '''
19
+
20
+
21
+ import java.util.Random;
22
+ import java.util.Scanner;
23
+
24
+
25
+
26
+ public class Test{
27
+ public static void main(String[] args){
28
+ final Random r = new Random();
29
+
30
+ Scanner reader = new Scanner(System.in);
31
+ System.out.print("Give a year: ");
32
+ int yr = Integer.valueOf(reader.nextLine());
33
+
34
+ boolean div4 = ((yr%4)==0);
35
+ boolean div100 = ((yr%100)==0);
36
+ boolean div400 = ((yr%400)==0);
37
+
38
+ if (div100) {
39
+ if (div400) {
40
+ System.out.println("Is a leap year");
41
+ }
42
+ else {
43
+ System.out.println("Not a leap year");
44
+ }
45
+ }
46
+ else if (div4) {
47
+ System.out.println("Is a leap year");
48
+ }
49
+ else {
50
+ System.out.println("Not a leap year");
51
+ }
52
+
53
+
54
+
55
+ }
56
+ }
57
+
58
+
59
+
60
+ '''Testing with input 1984
61
+ Give a year: 1984
62
+ Is a leap year
63
+
64
+ Testing with input 1983
65
+ Give a year: 1983
66
+ Not a leap year
67
+
68
+ Testing with input 1900
69
+ Give a year: 1900
70
+ Not a leap year
71
+
72
+ Testing with input 2000
73
+ Give a year: 2000
74
+ Is a leap year
75
+
76
+ Testing with input 2002
77
+ Give a year: 2002
78
+ Not a leap year
79
+
80
+ Testing with input 2004
81
+ Give a year: 2004
82
+ Is a leap year
83
+
84
+ Testing with input 2020
85
+ Give a year: 2020
86
+ Is a leap year
87
+
88
+ Testing with input 2018
89
+ Give a year: 2018
90
+ Not a leap year
91
+
92
+ Testing with input 1800
93
+ Give a year: 1800
94
+ Not a leap year
95
+
96
+ Testing with input 1700
97
+ Give a year: 1700
98
+ Not a leap year
99
+
100
+
101
+
102
+ '''