Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.
128 divides itself since 1, 2, and 8 all divide into 128 evenly.
public boolean dividesSelf(int n) {
for (; n>0;) {
if (n % n == 0)
return true;
n = n / 10;
}
return false;
}
Expected Runplease advise
dividesSelf(128) → true true OK
dividesSelf(12) → true true OK
dividesSelf(120) → false true X
dividesSelf(122) → true true OK
dividesSelf(13) → false true X
dividesSelf(32) → false true X
dividesSelf(22) → true true OK
dividesSelf(42) → false true X
dividesSelf(212) → true true OK
dividesSelf(213) → false true X
dividesSelf(162) → true true OK
other tests
X
Correct for more than half the tests
package com.solution;
public class DevideItself {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("valiue is-->"+dividesSelf(213));
}
public static boolean dividesSelf(int n) {
int k = 1;
boolean temp=false;
while (n > 0) {
if (n % 10 == 0)
return false;
else {
k = (n % 10);
if (n % k != 0)
return false;
else if(n%k==0&&!temp){
temp=true;
//return true;
}
//n = n / 10;
//return false;
// k=n/10;
}
}
return false;
}
}
public boolean dividesSelf(int n) {
int k = 1;
boolean temp=false;
while (n > 0) {
if (n % 10 == 0)
return false;
else {
k = (n % 10);
if (n % k != 0)
return false;
// else if(n%k==0&&temp){
else if(n%k==0){
//temp=true;
return true;
}
//n = n / 10;
//return false;
// k=n/10;
}
}
return false;
}
Expected Run
dividesSelf(128) → true true OK
dividesSelf(12) → true true OK
dividesSelf(120) → false false OK
dividesSelf(122) → true true OK
dividesSelf(13) → false false OK
dividesSelf(32) → false true X
dividesSelf(22) → true true OK
dividesSelf(42) → false true X
dividesSelf(212) → true true OK
dividesSelf(213) → false true X
dividesSelf(162) → true true OK
other tests
OK
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.