Find count of digits in a number that divide the number in C++


Suppose a number is given. We have to count number of digits of the number which evenly divides the number. Suppose the number is 1012, the result is 3. There are three digits 1, 1, and 2 that evenly divide 1012.

To solve this, we will find each digit of the number, using modulus operation, and check whether the number is divisible by that digit or not, if divisible, then increase counter. If the digit is 0, then ignore that digit.

Example

#include<iostream>
using namespace std;
   int countDivDigit(int num) {
   int count = 0;
   int temp = num;
   while(temp){
      int div = temp%10;
      if(div != 0){
         if(num % div == 0)
            count++;
      }
      temp /= 10;
   }
   return count;
}
int main() {
   int num = 1012;
   cout << "Number of digits that divides " << num << " evenly, is: " << countDivDigit(num);
}

Output

Number of digits that divides 1012 evenly, is: 3

Updated on: 01-Nov-2019

166 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements