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
Title | # Comments | Views | Activity |
---|---|---|---|
Running Jira on Raspberry PI 2? | 3 | 127 | |
PDFBox - convert image to PDF, PDF resolution | 3 | 67 | |
Scripting vs. Programming languages | 25 | 112 | |
Problem to start Neon | 20 | 53 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
20 Experts available now in Live!