📌  相关文章
📜  查找将数字相除的数字位数

📅  最后修改于: 2021-04-23 16:33:45             🧑  作者: Mango

给定正整数n 。任务是找到将数字n均分的数字位数。
例子:

Input : n = 12
Output : 2
1 and 2 divide 12.

Input : n = 1012
Output : 3
1, 1 and 2 divide 1012.

想法是找到数字n的每个数字除以模数10,然后检查是否除以n。因此,增加计数器。请注意,数字可以为0,因此请注意这种情况。
以下是此方法的实现:

C++
// C++ program to count number of digits
// that divides the number.
#include 
using namespace std;
 
// Return the number of digits that divides
// the number.
int countDigit(int n)
{
    int temp = n, count = 0;
    while (temp != 0) {
        // Fetching each digit of the number
        int d = temp % 10;
        temp /= 10;
 
        // Checking if digit is greater than 0
        // and can divides n.
        if (d > 0 && n % d == 0)
            count++;
    }
 
    return count;
}
 
// Driven Program
int main()
{
    int n = 1012;
 
    cout << countDigit(n) << endl;
    return 0;
}


Java
// Java program to count number of digits
// that divides the number.
 
class Test {
    // Return the number of digits that divides
    // the number.
    static int countDigit(int n)
    {
        int temp = n, count = 0;
        while (temp != 0) {
            // Fetching each digit of the number
            int d = temp % 10;
            temp /= 10;
 
            // Checking if digit is greater than 0
            // and can divides n.
            if (d > 0 && n % d == 0)
                count++;
        }
 
        return count;
    }
 
    // Driver method
    public static void main(String args[])
    {
        int n = 1012;
        System.out.println(countDigit(n));
    }
}


Python3
# Python3 code to count number of
# digits that divides the number.
 
# Return the number of digits
# that divides the number.
def countDigit (n):
    temp = n
    count = 0
    while temp != 0:
         
        # Fetching each digit
        # of the number
        d = temp % 10
        temp //= 10
     
        # Checking if digit is greater
        # than 0 and can divides n.
        if d > 0 and n % d == 0:
            count += 1
    return count
     
# Driven Code
n = 1012
print(countDigit(n))
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# program to count number of digits
// that divides the number.
using System;
 
class GFG {
 
    // Return the number of digits that
    // divides the number.
    static int countDigit(int n)
    {
        int temp = n, count = 0;
        while (temp != 0) {
 
            // Fetching each digit of
            // the number
            int d = temp % 10;
            temp /= 10;
 
            // Checking if digit is
            // greater than 0 and can
            // divides n.
            if (d > 0 && n % d == 0)
                count++;
        }
 
        return count;
    }
 
    // Driver method
    public static void Main()
    {
        int n = 1012;
 
        Console.Write(countDigit(n));
    }
}
 
// This code is contributed by parashar.


PHP
 0 && $n % $d == 0)
        $count++;
    }
 
    return $count;
}
 
    // Driver Code
    $n = 1012;
    echo countDigit($n), "\n";
     
// This code is contributed by ajit
?>


Javascript


输出:

3