Given a number n, task is to find that any of the digit in the number divides the number completely or not. Like we are given a number 128625 is divisible by 5 which is also present in the number.
Example
Input: 53142 Output: yes Explanation: This number is divisible by 1, 2 and 3 which are the digits of the number Input: 223 Output: No Explanation: The number is not divisible by either 2 or 3
The approach used below is as follows −
- We will start from the unit place and take the unit place’s number.
- Check whether the number is divisible or not
- Divide the number with 10
- Goto step 1 until the number is 0
Algorithm
Start In function int divisible(long long int n) Step 1-> Declare and initialize temp = n Step 2 -> Loop while n { Set k as n % 10 If temp % k == 0 then, Return 1 Set n = n/ 10 End loop Return 0 In Function int main() Step 1-> Declare and initialize n = 654123 Step 2-> If (divisible(n)) then, Print "Yes” Step 3-> Else Print "No”
Example
#include <stdio.h> int divisible(long long int n) { long long int temp = n; // check if any of digit divides n while (n) { int k = n % 10; if (temp % k == 0) return 1; n /= 10; } return 0; } int main() { long long int n = 654123; if (divisible(n)) { printf("Yes\n"); } else printf("No\n"); return 0; }
Output
If run the above code it will generate the following output −
Yes