Java > AP-1 > dividesSelf (CodingBat Solution)

Problem:

We'll say that a positive int divides itself if every digit in the number divides into the number evenly. So for example 128 divides itself since 1, 2, and 8 all divide into 128 evenly. We'll say that 0 does not divide into anything evenly, so no number with a 0 digit divides itself. Note: use % to get the rightmost digit, and / to discard the rightmost digit.

dividesSelf(128) → true
dividesSelf(12) → true
dividesSelf(120) → false

Solution:

public boolean dividesSelf(int n) {
int tmp = n;
if (n%10 == 0)
return false;
while(n != 0)
{
if (tmp % (n%10) != 0)
return false;
n /= 10;
}


return true;
}


15 comments :

  1. Another approach:

    public boolean dividesSelf(int n) {

    String val = String.valueOf(n);

    for (int i=0; i<val.length(); i++) {

    if (val.substring(i, i+1).equals("0"))

    return false;

    else if (n%Integer.valueOf(val.substring(i, i+1))!=0)

    return false;
    }
    return true;
    }

    ReplyDelete
  2. The solution does not work if you choose n = 105.

    ReplyDelete
  3. public boolean dividesSelf(int n) {
    boolean run = true;
    int b = n;
    while (b/10!=0||b>0)
    {
    if (b%10==0)
    return false;
    if (n%(b%10)!=0)
    return false;
    b=b/10;
    }
    return true;
    }

    ReplyDelete
  4. public boolean dividesSelf(int n) {
    String str= Integer.toString(n);

    for (int i =0; i< str.length(); i++)
    {
    char a= str.charAt(i);
    int c = Character.getNumericValue(a);
    if (c ==0)
    return false;

    if (n%c !=0)
    return false;

    }
    return true;
    }

    ReplyDelete
  5. public boolean dividesSelf(int n) {
    for (int i = n; i > 0; i = i / 10) {
    if (i % 10 == 0) return false;
    if (n % (i % 10) == 0) continue;
    return false;
    }
    return true;
    }

    ReplyDelete
  6. How About Recursive Solution :)

    public boolean dividesSelf(int n) {
    return check(n,n);
    }

    public boolean check(int num,int n){
    if(n==0)return true;
    if( (n%10)==0 || num%(n%10)!=0) return false;
    if(check(num,n/10))return true;
    return false;

    }

    ReplyDelete
  7. public boolean dividesSelf(int n) {
    int copy = n;
    while (n != 0) {
    if (n % 10 != 0 && (copy % (n % 10) == 0)) {
    n /= 10;
    } else {
    return false;
    }
    }
    return true;
    }

    ReplyDelete
  8. public boolean dividesSelf(int n) {
    return check(n,n);
    }

    private boolean check(int num,int n){
    if (n != 0) {
    if (n % 10 == 0 || num % (n % 10) != 0) {
    return false;
    }
    return check(num, n / 10);
    }
    return true;

    }

    ReplyDelete
  9. public boolean dividesSelf(int n) {
    return check(n,n);
    }

    private boolean check(int num,int n){
    if (n != 0) {
    if (n % 10 == 0 || num % (n % 10) != 0) {
    return false;
    }
    return check(num, n / 10);
    }
    return true;

    }

    ReplyDelete
  10. public boolean dividesSelf(int n) {

    for(int val = n; val != 0; val /= 10) {
    int digit = val % 10;
    if(digit == 0 || n % digit != 0)
    return false;
    }

    return true;
    }

    ReplyDelete
  11. if((n+"").contains("0")) return false;
    String s = String.valueOf(n);
    for(int i = 0; i<s.length(); i++) {
    double d = n / Double.parseDouble(s.charAt(i)+"");
    if(!String.valueOf(d).contains(".0")) return false;
    }
    return true;

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact