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.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

HackerRank Find Digits Solution
HackerRank Find Digits Solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

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

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

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

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

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

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

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))
    }
  }
}

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.

FAQ:

1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume. 

6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite. 

7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi


Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe. 

Sharing Is Caring

Leave a Comment