Or Smith
Or Smith

Reputation: 3616

Detect if number is a multiple of 7 or contains the number 7

I read a book about challenges in Java, and it gives the next question:

create a function, which get a number as argument, and detect if number is a multiple of 7 or contains the number 7.

The signature is: public boolean find7(int num)

I create this function, when the number is between 0 to 99, by the next condition:

if (num mod 7 == 0 || num / 10 ==7 || num mod 10 == 7)
   return true;

But what with number which is greater than 99? like 177, or 709? How can I detect it?

Upvotes: 2

Views: 4544

Answers (6)

joshreesjones
joshreesjones

Reputation: 1954

You can extend your approach to numbers above 100 like this:

public boolean find7(int num) {
    // support for negative integers
    num = Math.abs(num);

    // check if num is a multiple of 7
    if (num % 7 == 0) {
        return true;
    }

    // check to see if num contains 7
    while (num > 1) {
        // if the last digit is 7, return true
        if (num % 10 == 7) {
            return true;
        }

        // truncate the last digit
        num /= 10
    }

    // the number is not a multiple of 7 and it does not contain 7
    return false;
}

Upvotes: 2

arshajii
arshajii

Reputation: 129537

It's probably best to leave strings out of this:

public static boolean check(final int n) {
    int m = Math.abs(n);
    while (m > 0) {
        if (m % 10 == 7)
            return true;

        m /= 10;
    }

    return n % 7 == 0;
}

The while-loop checks each digit and tests if it is 7; if it is, we return true and if it isn't, we continue. We reach the final return statement only if none of the digits were 7, at which point we return whether the number is a multiple of 7.

Upvotes: 5

ps-aux
ps-aux

Reputation: 12176

For detecting if number is multiple of 7:

boolean isMultiple = x % 7 == 0

For detecting digit:

Convert it to String and use String.contains()

or

Create digit List like this:

private List<Integer> getDigitList(int number){
        List<Integer> list = new ArrayList<Integer>();

        int leftover = number;
        while (leftover != 0){
            int result = leftover % 10;
            leftover = (leftover - result)/10;
            list.add(result)
        }
        assert leftover == 0;

        return list;
    }

Upvotes: 0

Alexandros
Alexandros

Reputation: 2200

if (num % 7 ==0 || ("" + num).contains("7") ){
    return true;
}

Upvotes: 2

Aniket Thakur
Aniket Thakur

Reputation: 68975

You can do the following

    if(Integer.toString(num).contains("7") || ...){

    }

Upvotes: 1

Mureinik
Mureinik

Reputation: 311893

As far as checking if the number is divisible by 7, you're fine. If you want to check if it contains the 7 digit, I think the easiest approach would be to treat the number as a String:

public boolean find7(int num) {
    // check if it's a multiple of 7:
    if (num % 7 == 0) {
        return true;
    }

    // if it's not, check if it contains the character 7:
    return String.valueOf(num).contains("7");
}

Upvotes: 0

Related Questions