
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count 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
Advertisements