Hackerrank Find Digit problem solution

In this Hackerrank Find Digits problem we have given an integer, and for each digit that makes up the integer determine whether it is a divisor or not and we need to count the number of divisors that occur within the integer.


Problem solution in Python programming.

T = int(input())


for t in range(T):
    i_value = int(input())
    c_value = [float(i) for i in str(i_value)]
    
    total = 0
    for c in c_value:
        if c == 0:
            continue
        if (i_value/c)%1 == 0:
            total += 1
    print(total)


Problem solution in Java Programming.

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) {
    // Write your code here
int r = n;
int count = 0;
while(r > 0){
    if(r % 10 != 0 && n % (r % 10) == 0) count++;
    r = r / 10;
}
return count;

    }

}

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


Problem solution in C++ programming.

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

void solve() {
    int n;
    cin >> n;
    int nr = n;
    int counter = 0;
    while (nr > 0) {
        int c = nr % 10;
        nr = nr / 10;
        if (c != 0 && n % c == 0) {
            ++counter;
        }
    }
    cout << counter << endl;
}

int main() {
    int t;
    cin >> t;
    for (int i = 0; i < t; ++i) {
        solve();
    }
    return 0;
}


Problem solution in C programming.

#include <stdio.h>
int main()
{
    int i,t;
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
       unsigned long long n,n2;
       int i=0,term;
       scanf("%llu",&n);
       n2 = n;
       while(n2 > 0)
       {
           term = n2%10;
           if(term!= 0 && n%term==0) i++;
           n2 /= 10;
       }
       printf("%d\n",i);
    }
    return 0;
}


Problem solution in JavaScript programming.

function processData(numbers) {
  var numbers = numbers.split('\n');
  numbers.shift();
  
  var divs = numbers.map(function(n) {
    var count = 0;
    n.split('').forEach(function(d) {
      if(n%d === 0) {
        count++;
      }
    });
    return count;
  });
  
  console.log(divs.join('\n'));
  
} 

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

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


Post a Comment

0 Comments