Find Digits Problem – Hackerrank (Solution)

Given an integer, for each digit that makes up the integer determine whether it is a divisor. Count the number of divisors occurring within the integer.

Find the digits in this number that exactly divide N (division that leaves 0 as remainder) and display their count.

Sample Input: 
             12 
             1012

Sample Output:
             2
             3

Solution of Find Digits problem:

The number is broken into two digits, and . When is divided by either of those two digits, the remainder is 0, so they are both divisors.

  1. Number 12 is broken in two digits i.e. 1 and 2, so when given number 12 is divided by 1 & 2 and resultant remainder is 0 then output of program should be 2.
  2. Number 1012 is broken into four digits digits i.e. 1, 0,1 2, The given number 1012 is completely divisible by 1,1 and 2 and it can be divided by fourth digit 0 (it will throw and exception), so output of program would be 3.

Program: find digits problem in Java (HackerRank)

package org.learn.hr.algorithm;

public class FindDigits {
    static int findDigits(int n) {

        int ndivisor = 0;
        int orgNumber = n;
        while (n > 0) {
            int digit = n % 10;
            n = n / 10;
            if (digit == 0)
                continue;
            else if (orgNumber % digit == 0) {
                ndivisor++;
            }
        }
        return ndivisor;
    }

    public static void main(String[] args) {
        int number = 12;

        System.out.printf("Output for number: %d is %d\n", number, findDigits(number));

        number = 1012;
        System.out.printf("Output for number: %d is %d\n", number, findDigits(number));
    }
}

Output: find digit program in Java (HackerRank)

Output for number: 12 is 2
Output for number: 1012 is 3
Scroll to Top