-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCenturyFromYear.java
More file actions
38 lines (26 loc) · 819 Bytes
/
Copy pathCenturyFromYear.java
File metadata and controls
38 lines (26 loc) · 819 Bytes
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
package questions.codesignal.centuryfromyear;
public class CenturyFromYear {
public static int centuryFromYear(int year) {
// 1 to 100 years
if (year < 1) {
return -1;
}
// return 1st century
if (year <= 100) {
return 1;
}
// if the year is in multiples of 100
if (year % 100 == 0) {
return year / 100;
}
return (year / 100) + 1;
}
public static void main(String[] args) {
int year1 = 1905;
int century1 = centuryFromYear(year1);
System.out.println("Year " + year1 + " is in Century " + century1);
int year2 = 1700;
int century2 = centuryFromYear(year2);
System.out.println("Year " + year2 + " is in Century " + century2);
}
}