Find Digits Hackerrank Solution |Codeityweb

Find Digits Hackerrank Solution Java and Python

In this post we are going to discuss the Find Digits Hackerrank Solution using Java and Python as language of choice.

The Problem Statement is as Follows:

Given a integer find the count of digits that divide the integer.
 
Example: Suppose int i=12, the digits in the integer are 1 and 2 .
Now 12 is divisible by 2 and also it is divisible by 1 i.e 12%2==0 & 12&1==0 thus the count will be 2 and that will be the answer.
 
Suppose integer is 13, now digits are 1,3 . 13 is not divisible by 3 but divisible by 1 so the count will be 1 .So answer is 1.
 

Algorithm and Approach for Find Digits Hackerrank Solution:

  • First we need to get the digits from the integer for that we use a loop and take modulo by 10.
  • We check if the number is divisible by the digit and increment the counter.
  • We will use try and catch or try and except for Java and Python respectively to deal with ArithmeticException divide by zero since the integer may contain digits like 1202 has 0 as a digit so we need to catch it.
  • Finally return the total count.
 
 

Find Digits Hackerrank Solution Java:


static int findDigits(int n) {

int temp=n;
int count=0;

while(temp>0)
{
    int digit=temp%10;
    temp=temp/10;
    try{
        if(n%digit==0)
         {
            count++;
        }
    }
    catch(Exception e)
    {
        continue;
    }
}
return count;
}


Find Digit Hackerrank Solution Python:

def findDigits(n):
    temp=n
    count=0
    while temp>0:
    
    digit=temp%10
        temp=temp//10
    
    try:
    
        if n%digit==0:
    
            count=count+1
    
    except:
    
        continue
   return count

 

Output:

Find Digits Hackerrank Solution

 

Other Posts:

All Solutions At One Place:

Click Here

 

 Thank You For Reading!

 Any Doubts use comment section or contact form provided in the side bar.

Post a Comment

0 Comments