Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.
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 |
---|---|---|---|
web services creation SOAP vs REST | 5 | 37 | |
VbScript to countdown to New Year's Day | 6 | 54 | |
java stored proc example | 9 | 29 | |
runtime exception | 2 | 27 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
11 Experts available now in Live!