Find Digits HackerRank Solution in C, C++, Java, Python

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 into124  so return 3.

Check whether 1,1 , and 1 are divisors of 111. All 3 numbers divide evenly into 111 so return 3.

Check whether 1 and 0 are divisors of 10. 1 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 .

Sample Input

2

12

1012

 

Sample Output

2

3

 

Find Digits HackerRank 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 HackerRank Solution in C++

#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 HackerRank 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 HackerRank 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 HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
        
        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());
        }
    }
}

 

 

Attempt Find Digits HackerRank HackerRank Challenge 

Link – https://www.hackerrank.com/challenges/find-digits/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/extra-long-factorials-hackerrank-solution/

Leave a Comment