An integer d is a divisor of an integer n if the remainder of n%d=0.
Given an integer, for each digit that makes up the integer determine whether it is a divisor. Count the number of divisors occurring within the integer.
Example:
Input: n = 12
Output: 2
Approach
Java
public class FindDigits {public static void main(String[] args) {int n = 12;System.out.println(findDigits(n));}static int findDigits(int n) {int m, j, l, k, no, x;m = n;x = n;l = 0;while (x > 0) {l++;x = x / 10;}int a[] = new int[l];j = 0;while (n > 0) {a[j] = n % 10;n = n / 10;j++;}no = 0;for (k = 0; k < l; k++) {if (a[k] != 0) {if (m % a[k] == 0)no++;}}return no;}}
C++
#include <iostream>using namespace std;int findDigits(int n){int i, m, j, l, k, no, x;m = n;x = n;l = 0;while (x > 0){l++;x = x / 10;}long long a[l];j = 0;while (n > 0){a[j] = n % 10;n = n / 10;j++;}no = 0;for (k = 0; k < l; k++){if (a[k] != 0){if (m % a[k] == 0)no++;}}return no;}int main(){long long n = 12;cout << findDigits(n);return 0;}
No comments:
Post a Comment