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
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
split53 challenge | 7 | 79 | |
Windows Service to Receive TCP Packets | 4 | 124 | |
what is a "java.lang.System Property" ? | 20 | 57 | |
replace a word with other | 1 | 40 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
15 Experts available now in Live!