forked from HarryDulaney/intro-to-java-programming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExercise10_16.java
36 lines (31 loc) · 1.29 KB
/
Exercise10_16.java
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
package ch_10;
import java.math.BigDecimal;
/**
* 10.16 (Divisible by 2 or 3) Find the first ten numbers with 50 decimal digits that are
* divisible by 2 or 3.
*/
public class Exercise10_16 {
public static void main(String[] args) {
BigDecimal bigDecimal = new BigDecimal("10000000000000000000000000000000000000000000000000");
int count = 0;
while (count < 10) {
BigDecimal remainder2 = bigDecimal.remainder(BigDecimal.valueOf(2L));
if (remainder2.intValue() == 0) {
System.out.print("\n" + bigDecimal.toPlainString());
System.out.print(" divided by 2 = ");
BigDecimal res = bigDecimal.divide(BigDecimal.valueOf(2L));
System.out.print(res.toPlainString());
count++;
}
BigDecimal remainder3 = bigDecimal.remainder(BigDecimal.valueOf(3L));
if (remainder3.intValue() == 0) {
System.out.print("\n" + bigDecimal.toPlainString());
System.out.print(" divided by 3 = ");
BigDecimal res = bigDecimal.divide(BigDecimal.valueOf(3L));
System.out.print(res.toPlainString());
count++;
}
bigDecimal = bigDecimal.add(BigDecimal.ONE);
}
}
}