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:
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:
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.
0 Comments
Please Let me Know, If you have any doubts.