-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBirthDateLuckyNumber.java
More file actions
35 lines (23 loc) · 934 Bytes
/
BirthDateLuckyNumber.java
File metadata and controls
35 lines (23 loc) · 934 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
public class BirthDateLuckyNumber {
// Note: for simplicity date validation is not done
public static void main(String[] args) throws Exception {
// birthdate in DD-MM-YYYY format
String dob = "30-07-2021";
printLuckyNumber1(dob);
printLuckyNumber2(dob);
}
private static void printLuckyNumber1(String dob) {
String[] dobPart = dob.split("-");
int dd = Integer.parseInt(dobPart[0]);
int mm = Integer.parseInt(dobPart[1]);
int yyyy = Integer.parseInt(dobPart[2]);
int total = dd + mm + yyyy;
int luckyNum = ((total - 1) % 9) + 1;
System.out.printf("Logic1 :: DOB: %s, lucky number: %d\n", dob, luckyNum);
}
private static void printLuckyNumber2(String dob) {
int digits = Integer.parseInt(dob.replaceAll("-", "").replaceAll("0", ""));
int luckyNum = ((digits - 1) % 9) + 1;
System.out.printf("Logic2 :: DOB: %s, lucky number: %d\n", dob, luckyNum);
}
}