Find Digits Hackerrank problem solution

Find Digits Hackerrank

Given an integer, N, traverse its digits (d1,d2,…,dn) and determine how many digits evenly divide N(i.e.: count the number of times N divided by each digit di has a remainder of 0). Print the number of evenly divisible digits.

Note: Each digit is considered to be unique, so each occurrence of the same evenly divisible digit should be counted (i.e.: for N=111, the answer is 3).

Input Format

The first line is an integer, T, indicating the number of test cases.
The subsequent T lines each contain an integer, N.

1<=T<=15 1<=N<=109
Output Format

For every test case, count and print (on a new line) the number of digits in N that are able to evenly divide N.

Sample Input


Sample Output



The number 12 is broken into two digits, 1 and 2. When 12 is divided by either of those digits, the calculation’s remainder is 0; thus, the number of evenly-divisible digits in 12 is 2.

The number 1012 is broken into four digits, 1, 0, 1, and 2.1012 is evenly divisible by its digits 1, 1, and , but it is not divisible by 0 as division by zero is undefined; thus, our count of evenly divisible digits is 3.


Simply iterate through all digits; if the number is divisible by a digit, increase the counter.

Note that division by 0 is undefined and should be avoided. Otherwise, you’ll get Floating Point Exception error.

Refer to the code for clarity.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int n,value[15],i,j,x,count[15];
	if(n<=15 && n>=1) {
               if(j!=0 && value[i]%j==0)                 
        for(i=1;i<=n;i++) printf("%d\n",count[i]); 
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;

for _ in range(input()):
    a = input()
    count = 0
    for i in list(str(a)):
        if int(i) != 0 and a % int(i) == 0:
            count += 1
    print count
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

     public static void main(String[] args){
        int t;
        Scanner scan = new Scanner(;
        t = scan.nextInt();
        for(int i = 0; i < t; i++){

    private static int digits(String number) {
       int sum = 0;
       char[] digits = number.toCharArray();
       for(int i = 0; i < number.length(); i++){
            if(Character.getNumericValue(digits[i]) != 0){ 
                if(((Integer.parseInt(number))% (Character.getNumericValue(digits[i]))) == 0){
        return sum;
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int getR(string n, int q) {
    if(!q) return 0;
    int r = 0;
    for(int i = 0;i < n.length();++i) {
        r *= 10;
        r += (n[i] - '0');
        r %= q;
    if(!r) return 1;
    return 0;

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    int T;
    string n;
    int res = 0;
    cin >> T;
    while(T--) {
        cin >> n;
        res = 0;
        for(int i = 0;i < n.length();++i)
            res += getR(n, n[i] - '0');
        cout << res << endl;
    return 0;

A web developer(Front end and Back end), and DBA at Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me

Leave a reply:

Your email address will not be published.