C Programming: Count the digits in a number that divide it
C Programming Mathematics: Exercise-33 with Solution
Write a C program to count the digits in a given number that divide it.
Example:
Input (integer value) = 9
Digit of the said number is 9
9 divides 9
So, Output = 1
Input (integer value) = 27
Digits of the said number are 2, 7
2 cannot divides 27
7 cannot divides 27
So, Output = 0
Input (integer value) = 2245
Digits of the said number are 2, 2, 4, 5
2 cannot divides 2245
2 cannot divides 2245
4 cannot divides 2245
5 divides 2245
So, Output = 0
Test Data:
(9) -> 1
(27) -> 0
(145) -> 2
(2245) -> 1
(2222) -> 4
Sample Solution:
C Code:
#include <stdio.h>
int test(int n){
int temp = n;
int result = 0;
while(temp!=0){
int d = temp % 10;
temp/=10;
if(n % d == 0)
result++;
}
return result;
}
int main(void) {
int n = 9;
printf("n = %d",n);
printf("\nNumber of digits in the said number that divide it = %d", test(n));
n = 27;
printf("\nn = %d",n);
printf("\nNumber of digits in the said number that divide it = %d", test(n));
n = 145;
printf("\nn = %d",n);
printf("\nNumber of digits in the said number that divide it = %d", test(n));
n = 2245;
printf("\nn = %d",n);
printf("\nNumber of digits in the said number that divide it = %d", test(n));
n = 2222;
printf("\nn = %d",n);
printf("\nNumber of digits in the said number that divide it = %d", test(n));
}
Sample Output:
n = 9 Number of digits in the said number that divide it = 1 n = 27 Number of digits in the said number that divide it = 0 n = 145 Number of digits in the said number that divide it = 2 n = 2245 Number of digits in the said number that divide it = 1 n = 2222 Number of digits in the said number that divide it = 4
Flowchart:

C Programming Code Editor:
Improve this sample solution and post your code through Disqus.
Previous: Number sums and their reverses.
Next: Kth smallest number in a multiplication table.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
C Programming: Tips of the Day
What's the point of const pointers?
const is a tool which you should use in pursuit of a very important C++ concept:
Find bugs at compile-time, rather than run-time, by getting the compiler to enforce what you mean.
Even though it does not change the functionality, adding const generates a compiler error when you're doing things you didn't mean to do. Imagine the following typo:
void foo(int* ptr) { ptr = 0;// oops, I meant *ptr = 0 }
If you use int* const, this would generate a compiler error because you're changing the value to ptr. Adding restrictions via syntax is a good thing in general. Just don't take it too far -- the example you gave is a case where most people don't bother using const.
Ref : https://bit.ly/33Cdn3Q
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises