Skip to content
thecscience
THECSICENCE

Learn everything about computer science

  • Home
  • Human values
  • NCERT Solutions
  • HackerRank solutions
    • HackerRank Algorithms problems solutions
    • HackerRank C solutions
    • HackerRank C++ solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
thecscience
THECSICENCE

Learn everything about computer science

HackerRank Find Digits Problem Solution

Yashwant Parihar, April 16, 2023April 16, 2023

In this Post, we will solve HackerRank Find Digits Problem Solution.

An integer d is a divisor of an integer n if the remainder of n÷d = 0.
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.
Example
n = 124
Check whether 1, 2 and 4 are divisors of 124. All 3 numbers divide evenly into 124 so
return 3.
n = 111
Check whether 1, 1, and 1 are divisors of 111. All 3 numbers divide evenly into 111 so
return 3.
n = 10
Check whether 1 and 0 are divisors of 10. 1 is, but 0 is not. Return 1.

Function Description
Complete the find Digits function in the editor below.
findDigits has the following parameter(s):

  • int n: the value to analyze

Returns

  • int: the number of digits in n that are divisors of n

Input Format
The first line is an integer, t, the number of test cases. The t subsequent lines each contain an integer, n.
Constraints
1≤t≤15
0 < n < 10 power 9

Sample Input

2
12
1012

Sample Output

2
3

Explanation
The number 12 is broken into two digits, 1 and 2. When 12 is divided by either of those two digits, the remainder is 0 so they are both divisors.
The number 1012 is broken into four digits, 1, 0, 1, and 2. 1012 is evenly divisible by its digits 1, 1, and 2, but it is not divisible by 0 as division by zero is undefined.

HackerRank Find Digits Problem Solution
HackerRank Find Digits Problem Solution

Find Digits C Solution

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

int main() {
  
  int t,i,j,buffnum;
  char b, n[12];
  unsigned long modnum, num, cnt=0;
  
  scanf("%d",&t);
  for(i=0;i<t;i++){
    cnt = 0;
    scanf("%s",n);
    num=atoi(n);
    for(j=0;j<strlen(n);j++){
         buffnum = n[j] - '0';
      if(buffnum != 0){
       modnum=num%buffnum;
      		if(modnum == 0){
       			cnt += 1; 
      			}
    		}
    }
    printf("%lu\n",cnt);
 }
 
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}

Find Digits C++ Solution

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int number;
        cin >> number;
        int sum = 0;
        
        int loc = 0;
        while (number / pow(10, loc))
        {
            int digit = static_cast<int>(number / pow(10, loc++)) % 10;
            if (digit != 0 && number % digit == 0)
                sum++;
        }
        
        cout << sum << endl;
    }
    
    return 0;
}

Find Digits C Sharp Solution

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Solution
{

    static void Main(String[] args)
    {
        int numTestCases = Convert.ToInt32(Console.ReadLine());
        List<int> res = new List<int>();

        for(int i = 1; i <= numTestCases; i++)
        {
            int count = 0;
            string inputNumber = Console.ReadLine();
            long n = Convert.ToInt64(inputNumber);
            inputNumber = new string(inputNumber.Where(x => x != '0').ToArray());

            foreach(char c in inputNumber)
            {
                if(n % Convert.ToInt32(c.ToString()) == 0)
                {
                    count++;
                }
            }

             res.Add(count);
        }

        foreach (int r in res)
        {
            Console.WriteLine(r);
        }

        Console.ReadLine();

    }
}

Find Digits Java Solution

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'findDigits' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts INTEGER n as parameter.
     */

    public static int findDigits(int n) {
        String m = String.valueOf(n);
        int[] input = new int[m.length()];
        int forRet = 0;
        for(int i=0 ; i< m.length() ; i++){
            input[i] = (int) (m.charAt(i));
            input[i] -=48; 
            
            if(input[i] == 0){
                continue;
            }
            
            if( n%input[i]==0){
                forRet++;
            }
        }
        return forRet;

    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int t = Integer.parseInt(bufferedReader.readLine().trim());

        IntStream.range(0, t).forEach(tItr -> {
            try {
                int n = Integer.parseInt(bufferedReader.readLine().trim());

                int result = Result.findDigits(n);

                bufferedWriter.write(String.valueOf(result));
                bufferedWriter.newLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });

        bufferedReader.close();
        bufferedWriter.close();
    }
}

Find Digits JavaScript Solution

function processData(input) {


result = input.split("\n");
    for(number in result){
        if(result[++number] != result[result.length]){
            console.log(getoutput(result[number]));
        }
    }
}

function getoutput(str){
    
var totaldivis = 0;
    
    for(char in str ){
        if(str % str[char] == 0 ){
            totaldivis = totaldivis+1;
        }
}
    return totaldivis;
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

Find Digits Python Solution

T = int(input())
cases = [int(input()) for _ in range(T)]

for case in cases:
    count = 0
    rem = case
    while rem > 0:
        if rem % 10 != 0 and case % (rem % 10) == 0:
            count += 1
        rem //= 10
    print(count)

other solutions

  • HackerRank Extra Long Factorials Problem Solution
  • HackerRank Append and Delete Problem Solution
c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython

Post navigation

Previous post
Next post

Leave a Reply

You must be logged in to post a comment.

  • HackerRank Dynamic Array Problem Solution
  • HackerRank 2D Array – DS Problem Solution
  • Hackerrank Array – DS Problem Solution
  • Von Neumann and Harvard Machine Architecture
  • Development of Computers

Blogs that I follow

  • Programming
  • Data Structures
©2025 THECSICENCE | WordPress Theme by SuperbThemes