Find Digits HackerRank
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) {
int count=0;
int a=0;
String s=null;
s=String.valueOf(n);
for(int i=0;i<s.length();i++){
a=Integer.valueOf(String.valueOf(s.charAt(i)));
if(a!=0){
//System.out.println(a);
if((n/a)*a==n){ //You can also use modulus operator here n%a
count++;
}
}
}
System.out.println(count);
return count;
// Write your code here
}
}
Thanks for Reading.
Comments
Post a Comment