We are given with a number let’s say, num and the task is to calculate the divisor of a given number thereby count the divisors of num that have at least one digit common with n.
Input − num = 24
Output − Count is 4
Explanation − we will perform the following steps −
Firstly, calculate the divisors of a given number
Divisors of 24 are − 1, 2, 3, 4, 6, 8, 12, 24
Secondly, check which divisor have at least one digit that matches with the digits of a number
2, 4, 12, 24 are the divisors that contain the digit that matches with the digits in a number
Input − num = 10
Output − Count is 2
Explanation − we will perform the following steps −
Firstly, calculate the divisors of a given number
Divisors of 24 are: 1, 2, 5, 10
Secondly, check which divisor have at least one digit that matches with the digits of a number
1 and 10 are the divisors that contain the digit that matches with the digits in a number.
Example
package test; import java.util.*; import java.util.List; import java.util.Scanner; public class Testdigit{ static int digitCheck(int m,int arr[]){ while (m > 0){ if (arr[m % 10]==1){ return(1); } m = m / 10; } return(0); } public static void main (String[] args){ Scanner scan=new Scanner(System.in); int n=scan.nextInt(); int arr[] = new int[10]; int m = n; while (m > 0){ arr[m % 10] = 1; m = m / 10; } int count = 0; for (int i = 1; i <= Math.sqrt(n);i++){ if (n % i == 0){ if (digitCheck(i, arr)==1){ count++; } if (n / i != i){ if (digitCheck(n/i, arr)==1){ count++; } } } } System.out.println(ans); } }
Output
If we run the above code we will get the following output −
Enter any number: 24 Count 5