Skip to content

Commit f05c1c1

Browse files
committed
Solution to find the sum of digits and check if a number is a magic number.
1 parent 420f027 commit f05c1c1

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

src/Resursion/Power.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
public class Power {
44
public static void main(String[] args) {
5-
System.out.println(power(0,0));
6-
System.out.println(powerModulo(0,0,1));
5+
System.out.println(isMagicNumber(83557));
6+
// System.out.println(sumOfDigits(250));
7+
// System.out.println(power(0,0));
8+
// System.out.println(powerModulo(0,0,1));
79
}
810

911
static int power(int a, int p){
@@ -33,6 +35,36 @@ static long powerModulo(int A, int B, int C){
3335
}else {
3436
return (((A%C) * ((p*p)%C))%C);
3537
}
38+
}
39+
40+
static int sumOfDigits(int A){
41+
if (A == 0){
42+
return 0;
43+
}
44+
int division = A/10;
45+
int reminder = A%10;
46+
47+
return reminder + sumOfDigits(division);
48+
}
49+
50+
static int isMagicNumber(int A){
51+
52+
if (A == 0){
53+
return 0;
54+
}
55+
int division = A/10;
56+
int reminder = A%10;
57+
58+
int result = reminder + sumOfDigits(division);
59+
60+
while (result > 9){
61+
result = isMagicNumber(result);
62+
}
3663

64+
if (result != 1){
65+
return 0;
66+
}else {
67+
return 1;
68+
}
3769
}
3870
}

0 commit comments

Comments
 (0)