File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ package easy ;
2
+
3
+ /**
4
+ * Have the function AdditivePersistence(num) take the num parameter being passed
5
+ * which will always be a positive integer
6
+ * and return its additive persistence which is the number of times
7
+ * you must add the digits in num until you reach a single digit.
8
+ * ---
9
+ * For example: if num is 2718 then your program
10
+ * should return 2 because 2 + 7 + 1 + 8 = 18
11
+ * and 1 + 8 = 9, and you stop at 9.
12
+ */
13
+ public class AdditivePersistence {
14
+
15
+ /**
16
+ * Additive Persistence function.
17
+ *
18
+ * @param num input number
19
+ * @return additive persistence which is the number of times
20
+ */
21
+ private static int additivePersistence (int num ) {
22
+ int times = 0 ;
23
+ int added = num ;
24
+ while (added > 9 ) {
25
+ int sum = 0 ;
26
+ String [] intArr = Integer .toString (added ).split ("" );
27
+ for (String i : intArr ) {
28
+ sum += Integer .parseInt (i );
29
+ }
30
+ added = sum ;
31
+ times ++;
32
+ }
33
+ return times ;
34
+ }
35
+
36
+ /**
37
+ * Entry point.
38
+ *
39
+ * @param args command line arguments
40
+ */
41
+ public static void main (String [] args ) {
42
+ var result1 = additivePersistence (199 );
43
+ System .out .println (result1 );
44
+ var result2 = additivePersistence (913 );
45
+ System .out .println (result2 );
46
+ }
47
+
48
+ }
You can’t perform that action at this time.
0 commit comments