HackerRank Find Digits Solution

Hello Programmers, In this post, you will learn how to solve HackerRank Find Digits Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

HackerRank Find Digits Solution
HackerRank Find Digits Solution

HackerRank Find Digits

Task

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 12 and 4 are divisors of 124. All 3 numbers divide evenly into 124 so return 3.
n = 111

Check whether 11, 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 101 is, but 0 is not. Return 1.

Function Description

Complete the findDigits 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 < 109

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, 101, and 21012 is evenly divisible by its digits 11, and 2, but it is not divisible by 0 as division by zero is undefined.

HackerRank Find Digits Solution

HackerRank Find Digits Solution in C

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

int main() {
    long long T, N, j, status, i, dig_divisor = 0;
    status = scanf("%lld\n", &T);
    for(i = 0; i < T; i++){
        status = scanf("%lld\n", &N);
        j = N;
        while(j > 0){
            if(j % 10 == 0){
                j /= 10;
                continue;
            }
            if(N % (j % 10) == 0)
                dig_divisor++;
            j /= 10;
        }
        printf("%lld\n", dig_divisor);
        dig_divisor = 0;
    }
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}

HackerRank Find Digits Solution in Cpp

#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;
}

HackerRank Find Digits Solution in Java

import java.io.*;
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(System.in);
        
        t = scan.nextInt();
        
        for(int i = 0; i < t; i++){
            System.out.println(digits(scan.next()));
        }
        
        scan.close();
    }

    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){
                    sum++;
                }
            }
            else
                continue;
       }
           
        return sum;
    }
}

HackerRank Find Digits Solution in Python

# Enter your code here. Read input from STDIN. Print output to STDOUT
inputLines = int(raw_input())
for i in range(inputLines):
    total = 0
    number = int(raw_input())
    temp = number
    while number > 0:
        if number%10 != 0 and temp%(number%10)==0:
            total += 1
        number /= 10
    print total

HackerRank Find Digits Solution using JavaScript

function processData(input) {
    //Enter your code here
    var inputs=input.split('\n');
    var numberOfTCs=inputs[0];
    for(var i=1;i<=numberOfTCs;i++){
        var num=Number(inputs[i]);
        var count=0;
        while(num!=0){
            var digit=num%10;
            if(inputs[i]%digit==0)
                count++;
            num=Math.floor(num/10);
        }
        console.log(count);
    }
} 

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

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

HackerRank Find Digits Solution in Scala

object Solution {
  def main(args: Array[String]): Unit = {
    for (_ <- 0 until readInt()) {
      val n = readInt()
      val digits = "%d".format(n).map(c => (c - '0').toInt)
      println(digits.count(x => x != 0 && n % x == 0))
    }
  }
}

HackerRank Find Digits Solution in Pascal

program find_point;
var
	t, i, cnt, dig : integer;
	n, tmp : int64;

begin

	readln(t);
	for i:=1 to t do begin
		readln(n);
		tmp:=n;
		cnt:=0;
		while ( tmp > 0 ) do begin
			dig:=tmp mod 10;
			if ( dig <> 0 ) and ( n mod dig = 0 ) then
				inc(cnt);
			tmp:=tmp div 10;
		end;
		writeln(cnt);
	end;
	
end.

Disclaimer: This problem (Find Digits) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.

Sharing Is Caring