-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicNumber.java
More file actions
41 lines (38 loc) · 1.01 KB
/
Copy pathMagicNumber.java
File metadata and controls
41 lines (38 loc) · 1.01 KB
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
39
40
41
import java.util.Scanner;
public class MagicNumber {
int sumOfDigits(int no) {
int r, s = 0;
while (no > 0) {
r = no % 10;
s = s + r;
no = no / 10;
}
return s;
}
public void determineMagicNumber() {
Scanner sc = new Scanner(System.in);
System.out.print("ENTER A NUMBER [ INTEGER ] =");
int n = sc.nextInt();
int r, sum = n, temp = 0;
while (sum > 0) {
sum = sumOfDigits(n);
if (sum == 1) {
temp = 1;
break;
} else if (sum > 1 && sum <= 9) {
break;
} else {
n = sum;
}
}
if (temp == 1) {
System.out.print("Entered MAGIC NUMBER ");
} else {
System.out.print("Entered non-MAGIC NUMBER ");
}
}
public static void main(String[] args) {
MagicNumber magic = new MagicNumber();
magic.determineMagicNumber();
}
}