
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count digits in given number N which divide N in C++
We are given a number let’s say, N and the task is to find the count of those digits in a number that divides the number N.
Points to be remember
If the digit is 0 then it should be ignored which means count will not be increased for 0.
If a digit is appearing twice and it divides the number, then the count will depend upon the digit occurrence. For example, we are given a number 2240 and in this number every digit except the 0 will divide the number and 2 is occurring twice then the count will be 2 for digit 2.
Input − number = 2240
Output − count is 3
Explanation − Break the number into digits and it will be 2, 2, 4, 0. Now check whether 2 divides 2240 if yes then increase the count else move to next digit, in this number 2, 2, 4 divides the digit 2240 so count will be 3 and ignore the digit 0 in every case.
Input − number = 755
Output − count is 2
Explanation − Break the number into digits and it will be 7, 5, 5. Now check whether 7 divides 755 if yes then increase the count else move to next digit, in this number 5, 5 divides the digit 755 so count will be 2 and ignore the digit 0 in every case
Approach used in the below program is as follows
Input the number in a integer variable let’s say num
Start the loop till num is greater than 0
Inside the loop, break the number into digits and keep storing the results in a variable let’s say rem
Check if rem divides the number if yes then increase the value of a count variable by 1 and if not then don’t increase the value of a count variable.
And this check statement is applied when the rem is greater than 0 as we have to ignore the 0.
Example
#include <bits/stdc++.h> using namespace std; int count(int num){ int a=num; int count=0,rem; while(a>0){ rem=a%10; if(rem > 0){ if(num%rem==0){ count++; } } a=a/10; } return count; } int main(){ int num = 2240; cout<<"Count of digits in given number "<<num<<" which divide N are: "<<count(num); return 0; }
Output
If we run the above code, we will get the following output −
Count of digits in given number 2240 which divide N are: 3
- Related Articles
- Write a program in Python to count the number of digits in a given number N
- Count total number of digits from 1 to N in C++
- Count Numbers with N digits which consists of even number of 0's in C++
- Count Numbers with N digits which consists of odd number of 0's in C++
- Program to count number of stepping numbers of n digits in python
- Find count of digits in a number that divide the number in C++
- Count n digit numbers divisible by given number in C++
- Find N digits number which is divisible by D in C++
- Count of n digit numbers whose sum of digits equals to given sum in C++
- Smallest number after removing n digits in JavaScript
- Count ways to divide circle using N non-intersecting chords in C++
- Count number of bits changed after adding 1 to given N in C++
- How to count digits of given number? JavaScript
- Number of digits in 2 raised to power n in C++
- Number of digits that divide the complete number in JavaScript
