Competitive Coding

Find the Number Hackerrank Solution With Explanation

In this tutorial, we’ll learn how to find the Number Hackerrank Solution with an explanation. If you’re wondering how to find the number Hackerrank solution, then you’re at the right place. Here you’ll get all the Hackerrank solutions with explanations. But before getting started we’ll let you know about Hackerrank. So,

What is Hackerrank?

Hackerrank is a tech company that mainly focuses on competitive programming challenges for students, companies, and consumers. Hackerrank was founded by two founders Vivek Ravishankar and Harishankar Karunanidhi in 2012. Its headquarters are situated in California, United States.

With the help of this platform students as well as professionals prepare themselves for the placements by practicing the coding problems. The questions that hacker rank contains are asked in the interview rounds of many companies.

So let us see how to find the number Hackerrank solution with an Explanation.

Find the Number Hackerrank Problem

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 the 3 numbers divide evenly into 124 so return 3.

n = 111

check whether 1, 1, and 1 are divisors of 111. All the 3 numbers divide evenly into 111 so it returns 3.

n = 10

Now check whether 1 and 0 are divisors of 10. 1 is but the 0 is not so 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 subsequent t lines each contain an integer n.

Constraints

1 <= t <= 15

0 < n < 10^9

Sample Input

2
12
1012

Sample Output

2
3

Explanation

The number 12 is broken into the two digits, 1 and 2. And 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.

Find The Number Hackerrank solution in C

Now we’ll code the above problem with the help of the C language.

//Learnprogramo - programming made simple
#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;
    }
        
    return 0;
}

Output:

Explanation:

We’ve coded the above problem with the help of C language. So, after compiling the code the compiler will ask you to enter the numbers. After compiling the logic the desired output will be printed on the screen.

Simillary we’ll code the problem using different programming languages. Let’s see,

Find Numbers Hackerrank Solution in C++

//Learnprogramo - programming made simple
#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() {
     
    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;
}

Output:

Find the Number Hackerrank Solution in Java

//Learnprogramo - programmming made simple
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;
    }
}

Output:

Find the Number Hackerrank Solution Using Python

#learnprogramo - programming made simple
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

Output:

Find the Number Hackerrank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
        
        
        int T=Convert.ToInt32(Console.ReadLine());
        
        for (int i=0;i<T;i++){
            string broj=Console.ReadLine();
            int N=Convert.ToInt32(broj);
            int counter=0;       
            for (int j=0;j<broj.Length;j++){
                int znamenka=Convert.ToInt32(broj.Substring(j,1));   
                if (znamenka!=0 && N%znamenka==0){
                    counter++;
                }
            }          
            Console.WriteLine(counter.ToString());
        }
    }
}

Output:

Conclusion

So, in this way we’ve learned how to find the number Hackerrank solution with an explanation. If you’ve any doubts then please feel free to contact us. We’ll reach you as soon as possible.

Thanks and Happy Coding :)

Also Read:

This website uses cookies.