java - Algorithm for superdivide -
a int superdivide if every digit in number divides number. example 128
divides since 128
divisible 1, 2, , 8
superdivide number not divisible 0
.
sample input #1 superdivide(184) sample output #1 true sample input #2 superdivide(39) sample output #2 false sample input #3 superdivide(120) sample output #3 false
enter code here
public class superdivide { public static void main(string[] args) { superdivide obj = new superdivide(); boolean result = obj.checksuper(1001); system.out.println(result); } public boolean checksuper(int num){ //write code here int n1; int n2=num; if(num%10==0) return false; while(num>0){ n1=num%10; if(n2%n1==0){ num=num/10; return true; } } return false; }
above code runs fine upto digit number not 3digit,any suggestion?
maybe method should :
public boolean checksuper(int num) { int n1; int n2 = num; while(num > 0) { if(num % 10 == 0) { return false; } n1 = num % 10; if(n2 % n1 == 0) { num = num / 10; } else { return false; } } return true; }
i think work !
Comments
Post a Comment