Check if a number is divisible by any of its digits

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)
	{
		Divisibility task = 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
		task.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
		task.numDivisibleByDigitSum(423);
		// num = -113
		// -113 is not divisible by 3
		// -113 is divisible by 1
		// ---------------------
		// Output : Yes
		// Because digit 1 is divisible
		task.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
		task.numDivisibleByDigitSum(249);
		// num = 24
		// 24 is divisible by 2
		// 24 is divisible by 4
		// ---------------------
		// Output : Yes
		// Because digit [2,4] is divisible
		task.numDivisibleByDigitSum(24);
	}
}

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()
{
	Divisibility *task = 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
	task->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
	task->numDivisibleByDigitSum(423);
	// num = -113
	// -113 is not divisible by 3
	// -113 is divisible by 1
	// ---------------------
	// Output : Yes
	// Because digit 1 is divisible
	task->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
	task->numDivisibleByDigitSum(249);
	// num = 24
	// 24 is divisible by 2
	// 24 is divisible by 4
	// ---------------------
	// Output : Yes
	// Because digit [2,4] is divisible
	task->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
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
	task.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
	task.numDivisibleByDigitSum(423)
	// num = -113
	// -113 is not divisible by 3
	// -113 is divisible by 1
	// ---------------------
	// Output : Yes
	// Because digit 1 is divisible
	task.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
	task.numDivisibleByDigitSum(249)
	// num = 24
	// 24 is divisible by 2
	// 24 is divisible by 4
	// ---------------------
	// Output : Yes
	// Because digit [2,4] is divisible
	task.numDivisibleByDigitSum(24)
}

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)
	{
		Divisibility task = 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
		task.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
		task.numDivisibleByDigitSum(423);
		// num = -113
		// -113 is not divisible by 3
		// -113 is divisible by 1
		// ---------------------
		// Output : Yes
		// Because digit 1 is divisible
		task.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
		task.numDivisibleByDigitSum(249);
		// num = 24
		// 24 is divisible by 2
		// 24 is divisible by 4
		// ---------------------
		// Output : Yes
		// Because digit [2,4] is divisible
		task.numDivisibleByDigitSum(24);
	}
}

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()
{
	$task = 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
	$task->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
	$task->numDivisibleByDigitSum(423);
	// num = -113
	// -113 is not divisible by 3
	// -113 is divisible by 1
	// ---------------------
	// Output : Yes
	// Because digit 1 is divisible
	$task->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
	$task->numDivisibleByDigitSum(249);
	// num = 24
	// 24 is divisible by 2
	// 24 is divisible by 4
	// ---------------------
	// Output : Yes
	// Because digit [2,4] is divisible
	$task->numDivisibleByDigitSum(24);
}
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()
{
	var task = 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
	task.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
	task.numDivisibleByDigitSum(423);
	// num = -113
	// -113 is not divisible by 3
	// -113 is divisible by 1
	// ---------------------
	// Output : Yes
	// Because digit 1 is divisible
	task.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
	task.numDivisibleByDigitSum(249);
	// num = 24
	// 24 is divisible by 2
	// 24 is divisible by 4
	// ---------------------
	// Output : Yes
	// Because digit [2,4] is divisible
	task.numDivisibleByDigitSum(24);
}
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() :
	task = 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
	task.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
	task.numDivisibleByDigitSum(423)
	#  num = -113
	#  -113 is not divisible by 3
	#  -113 is divisible by 1
	#  ---------------------
	#  Output : Yes
	#  Because digit 1 is divisible
	task.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
	task.numDivisibleByDigitSum(249)
	#  num = 24
	#  24 is divisible by 2
	#  24 is divisible by 4
	#  ---------------------
	#  Output : Yes
	#  Because digit [2,4] is divisible
	task.numDivisibleByDigitSum(24)

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() 
	task = Divisibility.new()
	#  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
	task.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
	task.numDivisibleByDigitSum(423)
	#  num = -113
	#  -113 is not divisible by 3
	#  -113 is divisible by 1
	#  ---------------------
	#  Output : Yes
	#  Because digit 1 is divisible
	task.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
	task.numDivisibleByDigitSum(249)
	#  num = 24
	#  24 is divisible by 2
	#  24 is divisible by 4
	#  ---------------------
	#  Output : Yes
	#  Because digit [2,4] is divisible
	task.numDivisibleByDigitSum(24)
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
		task.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
		task.numDivisibleByDigitSum(423);
		// num = -113
		// -113 is not divisible by 3
		// -113 is divisible by 1
		// ---------------------
		// Output : Yes
		// Because digit 1 is divisible
		task.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
		task.numDivisibleByDigitSum(249);
		// num = 24
		// 24 is divisible by 2
		// 24 is divisible by 4
		// ---------------------
		// Output : Yes
		// Because digit [2,4] is divisible
		task.numDivisibleByDigitSum(24);
	}
}

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()
{
	let task: Divisibility = 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
	task.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
	task.numDivisibleByDigitSum(423);
	// num = -113
	// -113 is not divisible by 3
	// -113 is divisible by 1
	// ---------------------
	// Output : Yes
	// Because digit 1 is divisible
	task.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
	task.numDivisibleByDigitSum(249);
	// num = 24
	// 24 is divisible by 2
	// 24 is divisible by 4
	// ---------------------
	// Output : Yes
	// Because digit [2,4] is divisible
	task.numDivisibleByDigitSum(24);
}
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
{
	val task: Divisibility = 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
	task.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
	task.numDivisibleByDigitSum(423);
	// num = -113
	// -113 is not divisible by 3
	// -113 is divisible by 1
	// ---------------------
	// Output : Yes
	// Because digit 1 is divisible
	task.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
	task.numDivisibleByDigitSum(249);
	// num = 24
	// 24 is divisible by 2
	// 24 is divisible by 4
	// ---------------------
	// Output : Yes
	// Because digit [2,4] is divisible
	task.numDivisibleByDigitSum(24);
}

Output

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


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.

New Comment







© 2021, kalkicode.com, All rights reserved