Skip to content
Related Articles

Related Articles

Improve Article
Find count of digits in a number that divide the number
  • Difficulty Level : Easy
  • Last Updated : 05 Apr, 2021

Given a positive integer n. The task is to find count of digits of number which evenly divides the number n.
Examples: 
 

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

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

 

The idea is to find each digit of the number n by modulus 10 and then check whether it divides n or not. Accordingly, increment the counter. Notice that the digit can be 0, so take care of that case.
Below is implementation of this approach: 
 

C++




// C++ program to count number of digits
// that divides the number.
#include <bits/stdc++.h>
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




<?php
// PHP program to count
// number of digits
// that divides the number.
 
// Return the number of
// digits that divides
// the number.
function 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 && $n % $d == 0)
        $count++;
    }
 
    return $count;
}
 
    // Driver Code
    $n = 1012;
    echo countDigit($n), "\n";
     
// This code is contributed by ajit
?>

Javascript




<script>
// javascript program to count number of digits
// that divides the number. 
// Return the number of digits that divides
    // the number.
 
function countDigit(n)
{
    var temp = n, count = 0;
    while (temp != 0)
    {
     
        // Fetching each digit of the number
        var 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
var n = 1012;
document.write(countDigit(n));
     
// This code is contributed by Amit Katiyar
</script>

Output: 
 

3

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :