Posted on by Kalkicode
Code Number

# Check if a number is divisible by any of its digits

The problem at hand is to determine if a given number is divisible by any of its digits. To solve this problem, the provided code uses the approach of extracting the individual digits of the number and checking if the original number is divisible by any of these digits.

## Problem Statement

Given a positive or negative integer 'num,' we need to determine if 'num' is divisible by any of its digits. If any of its digits divides the number evenly, the function should return "Yes," otherwise "No."

## Explanation with Suitable Example

Let's take the example of the number 4273:

• Given number: 4273
• Digits: 4, 2, 7, 3

Now, we need to check if the number 4273 is divisible by any of its digits (4, 2, 7, or 3). None of the digits (4, 2, 7, or 3) divides 4273 evenly, so the output will be "No."

## Pseudocode and Algorithm Explanation

``````// Function to calculate absolute value of a number
function absValue(num):
if num < 0:
return -num
else:
return num

// Function to check if a number is divisible by any of its digits
function numDivisibleByDigitSum(num):
x = absValue(num) // Calculate the absolute value of 'num'
digits = [] // List to store individual digits of 'x'

// Extract the individual digits of 'x' and store them in 'digits'
while x > 0:
digits.append(x % 10)
x = x / 10

// Check if 'num' is divisible by any of its digits
for digit in digits:
if num % digit == 0:
print "Yes"
return

print "No"

// Test cases
numDivisibleByDigitSum(4273)
numDivisibleByDigitSum(423)
numDivisibleByDigitSum(-113)
numDivisibleByDigitSum(249)
numDivisibleByDigitSum(24)``````

The following approach to solve this problem:

1. Define a function called `absValue` that takes an integer 'num' as input and returns its absolute value.
2. Define a function called `numDivisibleByDigitSum` that takes an integer 'num' as input and performs the following steps: a. Calculate the absolute value of 'num' using the `absValue` function and store it in 'x'. b. Extract the individual digits of 'x' by repeatedly dividing it by 10 in a loop and taking the remainder (% operator). c. For each extracted digit, check if 'num' is divisible by that digit using the condition `num % (x % 10) == 0`. d. If any digit divides 'num' evenly, print "Yes" and return from the function. e. If no digit divides 'num' evenly, print "No."

## Code Solution

Here given code implementation process.

``````// C Program for
// Check if a number is divisible by any of its digits
#include <stdio.h>

int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
void numDivisibleByDigitSum(int num)
{
int x = absValue(num);
printf("\n Given number  : %d ", num);
// Sum of digit
while (x > 0)
{
if (num % (x % 10) == 0)
{
printf("\n Yes ");
return;
}
x = x / 10;
}
printf("\n No ");
}
int main(int argc, char
const *argv[])
{
// Test
// num = 4273
// 4273 is not divisible by 3
// 4273 is not divisible by 7
// 4273 is not divisible by 2
// 4273 is not divisible by 4
// ---------------------
// Output : No
// Because no digit is divisible
numDivisibleByDigitSum(4273);
// num = 423
// 423 is divisible by 3
// 423 is not divisible by 2
// 423 is not divisible by 4
// ---------------------
// Output : Yes
// Because digit 3 is divisible
numDivisibleByDigitSum(423);
// num = -113
// -113 is not divisible by 3
// -113 is divisible by 1
// ---------------------
// Output : Yes
// Because digit 1 is divisible
numDivisibleByDigitSum(-113);
// num = 249
// 249 is divisible by 9
// 249 is not divisible by 4
// 249 is not divisible by 2
// ---------------------
// Output : No
// Because no digit is divisible
numDivisibleByDigitSum(249);
// num = 24
// 24 is divisible by 2
// 24 is divisible by 4
// ---------------------
// Output : Yes
// Because digit [2,4] is divisible
numDivisibleByDigitSum(24);
return 0;
}``````

#### Output

`````` Given number  : 4273
No
Given number  : 423
Yes
Given number  : -113
Yes
Given number  : 249
No
Given number  : 24
Yes``````
``````// Java program for
// Check if a number is divisible by any of its digits
public class Divisibility
{
public int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
public void numDivisibleByDigitSum(int num)
{
int x = absValue(num);
System.out.print("\n Given number : " + num);
// Sum of digit
while (x > 0)
{
if (num % (x % 10) == 0)
{
System.out.print("\n Yes ");
return;
}
x = x / 10;
}
System.out.print("\n No ");
}
public static void main(String[] args)
{
// Test
// num = 4273
// 4273 is not divisible by 3
// 4273 is not divisible by 7
// 4273 is not divisible by 2
// 4273 is not divisible by 4
// ---------------------
// Output : No
// Because no digit is divisible
// num = 423
// 423 is divisible by 3
// 423 is not divisible by 2
// 423 is not divisible by 4
// ---------------------
// Output : Yes
// Because digit 3 is divisible
// num = -113
// -113 is not divisible by 3
// -113 is divisible by 1
// ---------------------
// Output : Yes
// Because digit 1 is divisible
// num = 249
// 249 is divisible by 9
// 249 is not divisible by 4
// 249 is not divisible by 2
// ---------------------
// Output : No
// Because no digit is divisible
// num = 24
// 24 is divisible by 2
// 24 is divisible by 4
// ---------------------
// Output : Yes
// Because digit [2,4] is divisible
}
}``````

#### Output

`````` Given number : 4273
No
Given number : 423
Yes
Given number : -113
Yes
Given number : 249
No
Given number : 24
Yes``````
``````// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Check if a number is divisible by any of its digits
class Divisibility
{
public: int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
void numDivisibleByDigitSum(int num)
{
int x = this->absValue(num);
cout << "\n Given number : " << num;
// Sum of digit
while (x > 0)
{
if (num % (x % 10) == 0)
{
cout << "\n Yes ";
return;
}
x = x / 10;
}
cout << "\n No ";
}
};
int main()
{
// Test
// num = 4273
// 4273 is not divisible by 3
// 4273 is not divisible by 7
// 4273 is not divisible by 2
// 4273 is not divisible by 4
// ---------------------
// Output : No
// Because no digit is divisible
// num = 423
// 423 is divisible by 3
// 423 is not divisible by 2
// 423 is not divisible by 4
// ---------------------
// Output : Yes
// Because digit 3 is divisible
// num = -113
// -113 is not divisible by 3
// -113 is divisible by 1
// ---------------------
// Output : Yes
// Because digit 1 is divisible
// num = 249
// 249 is divisible by 9
// 249 is not divisible by 4
// 249 is not divisible by 2
// ---------------------
// Output : No
// Because no digit is divisible
// num = 24
// 24 is divisible by 2
// 24 is divisible by 4
// ---------------------
// Output : Yes
// Because digit [2,4] is divisible
return 0;
}``````

#### Output

`````` Given number : 4273
No
Given number : 423
Yes
Given number : -113
Yes
Given number : 249
No
Given number : 24
Yes``````
``````package main
import "fmt"
// Go program for
// Check if a number is divisible by any of its digits
type Divisibility struct {}
func getDivisibility() * Divisibility {
var me *Divisibility = &Divisibility {}
return me
}
func(this Divisibility) absValue(num int) int {
if num < 0 {
return -num
}
return num
}
func(this Divisibility) numDivisibleByDigitSum(num int) {
var x int = this.absValue(num)
fmt.Print("\n Given number : ", num)
// Sum of digit
for (x > 0) {
if num % (x % 10) == 0 {
fmt.Print("\n Yes ")
return
}
x = x / 10
}
fmt.Print("\n No ")
}
func main() {
var task * Divisibility = getDivisibility()
// Test
// num = 4273
// 4273 is not divisible by 3
// 4273 is not divisible by 7
// 4273 is not divisible by 2
// 4273 is not divisible by 4
// ---------------------
// Output : No
// Because no digit is divisible
// num = 423
// 423 is divisible by 3
// 423 is not divisible by 2
// 423 is not divisible by 4
// ---------------------
// Output : Yes
// Because digit 3 is divisible
// num = -113
// -113 is not divisible by 3
// -113 is divisible by 1
// ---------------------
// Output : Yes
// Because digit 1 is divisible
// num = 249
// 249 is divisible by 9
// 249 is not divisible by 4
// 249 is not divisible by 2
// ---------------------
// Output : No
// Because no digit is divisible
// num = 24
// 24 is divisible by 2
// 24 is divisible by 4
// ---------------------
// Output : Yes
// Because digit [2,4] is divisible
}``````

#### Output

`````` Given number : 4273
No
Given number : 423
Yes
Given number : -113
Yes
Given number : 249
No
Given number : 24
Yes``````
``````// Include namespace system
using System;
// Csharp program for
// Check if a number is divisible by any of its digits
public class Divisibility
{
public int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
public void numDivisibleByDigitSum(int num)
{
int x = this.absValue(num);
Console.Write("\n Given number : " + num);
// Sum of digit
while (x > 0)
{
if (num % (x % 10) == 0)
{
Console.Write("\n Yes ");
return;
}
x = x / 10;
}
Console.Write("\n No ");
}
public static void Main(String[] args)
{
// Test
// num = 4273
// 4273 is not divisible by 3
// 4273 is not divisible by 7
// 4273 is not divisible by 2
// 4273 is not divisible by 4
// ---------------------
// Output : No
// Because no digit is divisible
// num = 423
// 423 is divisible by 3
// 423 is not divisible by 2
// 423 is not divisible by 4
// ---------------------
// Output : Yes
// Because digit 3 is divisible
// num = -113
// -113 is not divisible by 3
// -113 is divisible by 1
// ---------------------
// Output : Yes
// Because digit 1 is divisible
// num = 249
// 249 is divisible by 9
// 249 is not divisible by 4
// 249 is not divisible by 2
// ---------------------
// Output : No
// Because no digit is divisible
// num = 24
// 24 is divisible by 2
// 24 is divisible by 4
// ---------------------
// Output : Yes
// Because digit [2,4] is divisible
}
}``````

#### Output

`````` Given number : 4273
No
Given number : 423
Yes
Given number : -113
Yes
Given number : 249
No
Given number : 24
Yes``````
``````<?php
// Php program for
// Check if a number is divisible by any of its digits
class Divisibility
{
public	function absValue(\$num)
{
if (\$num < 0)
{
return -\$num;
}
return \$num;
}
public	function numDivisibleByDigitSum(\$num)
{
\$x = \$this->absValue(\$num);
echo("\n Given number : ".\$num);
// Sum of digit
while (\$x > 0)
{
if (\$num % (\$x % 10) == 0)
{
echo("\n Yes ");
return;
}
\$x = (int)(\$x / 10);
}
echo("\n No ");
}
}

function main()
{
// Test
// num = 4273
// 4273 is not divisible by 3
// 4273 is not divisible by 7
// 4273 is not divisible by 2
// 4273 is not divisible by 4
// ---------------------
// Output : No
// Because no digit is divisible
// num = 423
// 423 is divisible by 3
// 423 is not divisible by 2
// 423 is not divisible by 4
// ---------------------
// Output : Yes
// Because digit 3 is divisible
// num = -113
// -113 is not divisible by 3
// -113 is divisible by 1
// ---------------------
// Output : Yes
// Because digit 1 is divisible
// num = 249
// 249 is divisible by 9
// 249 is not divisible by 4
// 249 is not divisible by 2
// ---------------------
// Output : No
// Because no digit is divisible
// num = 24
// 24 is divisible by 2
// 24 is divisible by 4
// ---------------------
// Output : Yes
// Because digit [2,4] is divisible
}
main();``````

#### Output

`````` Given number : 4273
No
Given number : 423
Yes
Given number : -113
Yes
Given number : 249
No
Given number : 24
Yes``````
``````// Node JS program for
// Check if a number is divisible by any of its digits
class Divisibility
{
absValue(num)
{
if (num < 0)
{
return -num;
}
return num;
}
numDivisibleByDigitSum(num)
{
var x = this.absValue(num);
process.stdout.write("\n Given number : " + num);
// Sum of digit
while (x > 0)
{
if (num % (x % 10) == 0)
{
process.stdout.write("\n Yes ");
return;
}
x = parseInt(x / 10);
}
process.stdout.write("\n No ");
}
}

function main()
{
// Test
// num = 4273
// 4273 is not divisible by 3
// 4273 is not divisible by 7
// 4273 is not divisible by 2
// 4273 is not divisible by 4
// ---------------------
// Output : No
// Because no digit is divisible
// num = 423
// 423 is divisible by 3
// 423 is not divisible by 2
// 423 is not divisible by 4
// ---------------------
// Output : Yes
// Because digit 3 is divisible
// num = -113
// -113 is not divisible by 3
// -113 is divisible by 1
// ---------------------
// Output : Yes
// Because digit 1 is divisible
// num = 249
// 249 is divisible by 9
// 249 is not divisible by 4
// 249 is not divisible by 2
// ---------------------
// Output : No
// Because no digit is divisible
// num = 24
// 24 is divisible by 2
// 24 is divisible by 4
// ---------------------
// Output : Yes
// Because digit [2,4] is divisible
}
main();``````

#### Output

`````` Given number : 4273
No
Given number : 423
Yes
Given number : -113
Yes
Given number : 249
No
Given number : 24
Yes``````
``````#  Python 3 program for
#  Check if a number is divisible by any of its digits
class Divisibility :
def absValue(self, num) :
if (num < 0) :
return -num

return num

def numDivisibleByDigitSum(self, num) :
x = self.absValue(num)
print("\n Given number : ", num, end = "")
#  Sum of digit
while (x > 0) :
if (num % (x % 10) == 0) :
print("\n Yes ", end = "")
return

x = int(x / 10)

print("\n No ", end = "")

def main() :
#  Test
#  num = 4273
#  4273 is not divisible by 3
#  4273 is not divisible by 7
#  4273 is not divisible by 2
#  4273 is not divisible by 4
#  ---------------------
#  Output : No
#  Because no digit is divisible
#  num = 423
#  423 is divisible by 3
#  423 is not divisible by 2
#  423 is not divisible by 4
#  ---------------------
#  Output : Yes
#  Because digit 3 is divisible
#  num = -113
#  -113 is not divisible by 3
#  -113 is divisible by 1
#  ---------------------
#  Output : Yes
#  Because digit 1 is divisible
#  num = 249
#  249 is divisible by 9
#  249 is not divisible by 4
#  249 is not divisible by 2
#  ---------------------
#  Output : No
#  Because no digit is divisible
#  num = 24
#  24 is divisible by 2
#  24 is divisible by 4
#  ---------------------
#  Output : Yes
#  Because digit [2,4] is divisible

if __name__ == "__main__": main()``````

#### Output

`````` Given number :  4273
No
Given number :  423
Yes
Given number :  -113
Yes
Given number :  249
No
Given number :  24
Yes``````
``````#  Ruby program for
#  Check if a number is divisible by any of its digits
class Divisibility
def absValue(num)
if (num < 0)
return -num
end

return num
end

def numDivisibleByDigitSum(num)
x = self.absValue(num)
print("\n Given number : ", num)
#  Sum of digit
while (x > 0)
if (num % (x % 10) == 0)
print("\n Yes ")
return
end

x = x / 10
end

print("\n No ")
end

end

def main()
#  Test
#  num = 4273
#  4273 is not divisible by 3
#  4273 is not divisible by 7
#  4273 is not divisible by 2
#  4273 is not divisible by 4
#  ---------------------
#  Output : No
#  Because no digit is divisible
#  num = 423
#  423 is divisible by 3
#  423 is not divisible by 2
#  423 is not divisible by 4
#  ---------------------
#  Output : Yes
#  Because digit 3 is divisible
#  num = -113
#  -113 is not divisible by 3
#  -113 is divisible by 1
#  ---------------------
#  Output : Yes
#  Because digit 1 is divisible
#  num = 249
#  249 is divisible by 9
#  249 is not divisible by 4
#  249 is not divisible by 2
#  ---------------------
#  Output : No
#  Because no digit is divisible
#  num = 24
#  24 is divisible by 2
#  24 is divisible by 4
#  ---------------------
#  Output : Yes
#  Because digit [2,4] is divisible
end

main()``````

#### Output

`````` Given number : 4273
No
Given number : 423
Yes
Given number : -113
Yes
Given number : 249
No
Given number : 24
Yes ``````
``````// Scala program for
// Check if a number is divisible by any of its digits
class Divisibility()
{
def absValue(num: Int): Int = {
if (num < 0)
{
return -num;
}
return num;
}
def numDivisibleByDigitSum(num: Int): Unit = {
var x: Int = absValue(num);
print("\n Given number : " + num);
// Sum of digit
while (x > 0)
{
if (num % (x % 10) == 0)
{
print("\n Yes ");
return;
}
x = x / 10;
}
print("\n No ");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Divisibility = new Divisibility();
// Test
// num = 4273
// 4273 is not divisible by 3
// 4273 is not divisible by 7
// 4273 is not divisible by 2
// 4273 is not divisible by 4
// ---------------------
// Output : No
// Because no digit is divisible
// num = 423
// 423 is divisible by 3
// 423 is not divisible by 2
// 423 is not divisible by 4
// ---------------------
// Output : Yes
// Because digit 3 is divisible
// num = -113
// -113 is not divisible by 3
// -113 is divisible by 1
// ---------------------
// Output : Yes
// Because digit 1 is divisible
// num = 249
// 249 is divisible by 9
// 249 is not divisible by 4
// 249 is not divisible by 2
// ---------------------
// Output : No
// Because no digit is divisible
// num = 24
// 24 is divisible by 2
// 24 is divisible by 4
// ---------------------
// Output : Yes
// Because digit [2,4] is divisible
}
}``````

#### Output

`````` Given number : 4273
No
Given number : 423
Yes
Given number : -113
Yes
Given number : 249
No
Given number : 24
Yes``````
``````// Swift 4 program for
// Check if a number is divisible by any of its digits
class Divisibility
{
func absValue(_ num: Int) -> Int
{
if (num < 0)
{
return -num;
}
return num;
}
func numDivisibleByDigitSum(_ num: Int)
{
var x: Int = self.absValue(num);
print("\n Given number : ", num, terminator: "");
// Sum of digit
while (x > 0)
{
if (num % (x % 10) == 0)
{
print("\n Yes ", terminator: "");
return;
}
x = x / 10;
}
print("\n No ", terminator: "");
}
}
func main()
{
// Test
// num = 4273
// 4273 is not divisible by 3
// 4273 is not divisible by 7
// 4273 is not divisible by 2
// 4273 is not divisible by 4
// ---------------------
// Output : No
// Because no digit is divisible
// num = 423
// 423 is divisible by 3
// 423 is not divisible by 2
// 423 is not divisible by 4
// ---------------------
// Output : Yes
// Because digit 3 is divisible
// num = -113
// -113 is not divisible by 3
// -113 is divisible by 1
// ---------------------
// Output : Yes
// Because digit 1 is divisible
// num = 249
// 249 is divisible by 9
// 249 is not divisible by 4
// 249 is not divisible by 2
// ---------------------
// Output : No
// Because no digit is divisible
// num = 24
// 24 is divisible by 2
// 24 is divisible by 4
// ---------------------
// Output : Yes
// Because digit [2,4] is divisible
}
main();``````

#### Output

`````` Given number :  4273
No
Given number :  423
Yes
Given number :  -113
Yes
Given number :  249
No
Given number :  24
Yes``````
``````// Kotlin program for
// Check if a number is divisible by any of its digits
class Divisibility
{
fun absValue(num: Int): Int
{
if (num < 0)
{
return -num;
}
return num;
}
fun numDivisibleByDigitSum(num: Int): Unit
{
var x: Int = this.absValue(num);
print("\n Given number : " + num);
// Sum of digit
while (x > 0)
{
if (num % (x % 10) == 0)
{
print("\n Yes ");
return;
}
x = x / 10;
}
print("\n No ");
}
}
fun main(args: Array < String > ): Unit
{
// Test
// num = 4273
// 4273 is not divisible by 3
// 4273 is not divisible by 7
// 4273 is not divisible by 2
// 4273 is not divisible by 4
// ---------------------
// Output : No
// Because no digit is divisible
// num = 423
// 423 is divisible by 3
// 423 is not divisible by 2
// 423 is not divisible by 4
// ---------------------
// Output : Yes
// Because digit 3 is divisible
// num = -113
// -113 is not divisible by 3
// -113 is divisible by 1
// ---------------------
// Output : Yes
// Because digit 1 is divisible
// num = 249
// 249 is divisible by 9
// 249 is not divisible by 4
// 249 is not divisible by 2
// ---------------------
// Output : No
// Because no digit is divisible
// num = 24
// 24 is divisible by 2
// 24 is divisible by 4
// ---------------------
// Output : Yes
// Because digit [2,4] is divisible
}``````

#### Output

`````` Given number : 4273
No
Given number : 423
Yes
Given number : -113
Yes
Given number : 249
No
Given number : 24
Yes``````

## Resultant Output Explanation

The code is then tested with multiple test cases:

1. numDivisibleByDigitSum(4273):

• Digits: 4, 2, 7, 3
• None of the digits divides 4273 evenly, so the output is "No."
2. numDivisibleByDigitSum(423):

• Digits: 4, 2, 3
• The digit 3 divides 423 evenly, so the output is "Yes."
3. numDivisibleByDigitSum(-113):

• Digits: 1, 1, 3
• The digit 1 divides -113 evenly, so the output is "Yes."
4. numDivisibleByDigitSum(249):

• Digits: 2, 4, 9
• None of the digits divides 249 evenly, so the output is "No."
5. numDivisibleByDigitSum(24):

• Digits: 2, 4
• Both digits 2 and 4 divide 24 evenly, so the output is "Yes."

## Time Complexity

The time complexity of the provided code is primarily determined by the number of digits in the input 'num.' Let's call this number 'd,' and the value of 'num' as 'n.'

The process of extracting digits from 'num' takes O(d) time, and the subsequent check for divisibility takes O(d) time as well. Therefore, the overall time complexity of the code is O(d).

In the worst case, when 'num' has 'd' digits, the time complexity is O(d), which is linear in the number of digits.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post