-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMagic.java
54 lines (54 loc) · 1.03 KB
/
Magic.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//Magic Number
import java.util.*;
class Magic{
private int n;
Magic(){
n=0;
}
void accept(){
Scanner sc=new Scanner(System.in);
do{
System.out.println("Enter the number");
n=sc.nextInt();
}while(n<=9);
for(int i=n;;i++){
if(isMagic(i)){
System.out.println("Magic number: "+i);
break;
}
}
}
boolean isMagic(int x){
int s=x;
for(int i=1;;i++){
s=sum(s);
if((count(s))==1){
if(s==1)
return true;
else
return false;
}
}
}
int sum(int x){
int t=0;
while(x>0){
t+=(x%10);
x/=10;
}
return t;
}
int count(int x){
int c=0;
while(x>0){
int b=x%10;
c++;
x/=10;
}
return c;
}
public static void main(String args[]){
Magic ob=new Magic();
ob.accept();
}
}