Check if the sum of digits of number is divisible by all of its digits

Here given code implementation process.

// C Program for
// Check if the sum of digits of number is divisible by all of its digits
#include <stdio.h>

int absValue(int num)
{
	if (num < 0)
	{
		return -num;
	}
	return num;
}
void isDigitsSumDivisible(int num)
{
	int x = absValue(num);
	int sum = 0;
	printf("\n Given number  : %d ", num);
	// Sum of digit
	while (x > 0)
	{
		sum += (x % 10);
		x = x / 10;
	}
	x = absValue(num);
	// Check whether digit of number is divisible by sum of digits sum
	while (x > 0)
	{
		if (sum % (x % 10) != 0)
		{
			// When digit is not divisible
			printf("\n No ");
			return;
		}
		x = x / 10;
	}
	// When all digit is divisible
	printf("\n Yes ");
}
int main(int argc, char
	const *argv[])
{
	// Test
	// num = 24312
	// digit sum = [2 + 4 + 3 + 1 + 2] = (12)
	// 12 is divisible by 2
	// 12 is divisible by 4
	// 12 is divisible by 3
	// 12 is divisible by 1
	// 12 is divisible by 2
	// ---------------------
	// Output : Yes
	isDigitsSumDivisible(24312);
	// num = 21321
	// digit sum = [2+1+3+2+1] = (9)
	// 9 is not divisible by 2
	// ---------------------
	// Output : No
	isDigitsSumDivisible(21321);
	// num = 1221
	// digit sum = [1+2+2+1] = (6)
	// 6 is divisible by 1
	// 6 is divisible by 2
	// 6 is divisible by 2
	// 6 is divisible by 1
	// ---------------------
	// Output : Yes
	isDigitsSumDivisible(1221);
	return 0;
}

Output

 Given number  : 24312
 Yes
 Given number  : 21321
 No
 Given number  : 1221
 Yes
// Java program for
// Check if the sum of digits of number is divisible by all of its digits
public class Divisibility
{
	public int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	public void isDigitsSumDivisible(int num)
	{
		int x = absValue(num);
		int sum = 0;
		System.out.print("\n Given number : " + num );
		// Sum of digit
		while (x > 0)
		{
			sum += (x % 10);
			x = x / 10;
		}
		x = absValue(num);
		// Check whether digit of number is divisible by sum of digits sum
		while (x > 0)
		{
			if (sum % (x % 10) != 0)
			{
				// When digit is not divisible
				System.out.print("\n No ");
				return;
			}
			x = x / 10;
		}
		// When all digit is divisible
		System.out.print("\n Yes ");
	}
	public static void main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		// num = 24312
		// digit sum = [2 + 4 + 3 + 1 + 2] = (12)
		// 12 is divisible by 2
		// 12 is divisible by 4
		// 12 is divisible by 3
		// 12 is divisible by 1
		// 12 is divisible by 2
		// ---------------------
		// Output : Yes
		task.isDigitsSumDivisible(24312);
		// num = 21321
		// digit sum = [2+1+3+2+1] = (9)
		// 9 is not divisible by 2
		// ---------------------
		// Output : No
		task.isDigitsSumDivisible(21321);
		// num = 1221
		// digit sum = [1+2+2+1] = (6)
		// 6 is divisible by 1
		// 6 is divisible by 2
		// 6 is divisible by 2
		// 6 is divisible by 1
		// ---------------------
		// Output : Yes
		task.isDigitsSumDivisible(1221);
	}
}

Output

 Given number : 24312
 Yes
 Given number : 21321
 No
 Given number : 1221
 Yes
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Check if the sum of digits of number is divisible by all of its digits
class Divisibility
{
	public: int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	void isDigitsSumDivisible(int num)
	{
		int x = this->absValue(num);
		int sum = 0;
		cout << "\n Given number : " << num;
		// Sum of digit
		while (x > 0)
		{
			sum += (x % 10);
			x = x / 10;
		}
		x = this->absValue(num);
		// Check whether digit of number is divisible by sum of digits sum
		while (x > 0)
		{
			if (sum % (x % 10) != 0)
			{
				// When digit is not divisible
				cout << "\n No ";
				return;
			}
			x = x / 10;
		}
		// When all digit is divisible
		cout << "\n Yes ";
	}
};
int main()
{
	Divisibility *task = new Divisibility();
	// Test
	// num = 24312
	// digit sum = [2 + 4 + 3 + 1 + 2] = (12)
	// 12 is divisible by 2
	// 12 is divisible by 4
	// 12 is divisible by 3
	// 12 is divisible by 1
	// 12 is divisible by 2
	// ---------------------
	// Output : Yes
	task->isDigitsSumDivisible(24312);
	// num = 21321
	// digit sum = [2+1+3+2+1] = (9)
	// 9 is not divisible by 2
	// ---------------------
	// Output : No
	task->isDigitsSumDivisible(21321);
	// num = 1221
	// digit sum = [1+2+2+1] = (6)
	// 6 is divisible by 1
	// 6 is divisible by 2
	// 6 is divisible by 2
	// 6 is divisible by 1
	// ---------------------
	// Output : Yes
	task->isDigitsSumDivisible(1221);
	return 0;
}

Output

 Given number : 24312
 Yes
 Given number : 21321
 No
 Given number : 1221
 Yes
package main
import "fmt"
// Go program for
// Check if the sum of digits of number is divisible by all 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) isDigitsSumDivisible(num int) {
	var x int = this.absValue(num)
	var sum int = 0
	fmt.Print("\n Given number : ", num)
	// Sum of digit
	for (x > 0) {
		sum += (x % 10)
		x = x / 10
	}
	x = this.absValue(num)
	// Check whether digit of number is divisible by sum of digits sum
	for (x > 0) {
		if sum % (x % 10) != 0 {
			// When digit is not divisible
			fmt.Print("\n No ")
			return
		}
		x = x / 10
	}
	// When all digit is divisible
	fmt.Print("\n Yes ")
}
func main() {
	var task * Divisibility = getDivisibility()
	// Test
	// num = 24312
	// digit sum = [2 + 4 + 3 + 1 + 2] = (12)
	// 12 is divisible by 2
	// 12 is divisible by 4
	// 12 is divisible by 3
	// 12 is divisible by 1
	// 12 is divisible by 2
	// ---------------------
	// Output : Yes
	task.isDigitsSumDivisible(24312)
	// num = 21321
	// digit sum = [2+1+3+2+1] = (9)
	// 9 is not divisible by 2
	// ---------------------
	// Output : No
	task.isDigitsSumDivisible(21321)
	// num = 1221
	// digit sum = [1+2+2+1] = (6)
	// 6 is divisible by 1
	// 6 is divisible by 2
	// 6 is divisible by 2
	// 6 is divisible by 1
	// ---------------------
	// Output : Yes
	task.isDigitsSumDivisible(1221)
}

Output

 Given number : 24312
 Yes
 Given number : 21321
 No
 Given number : 1221
 Yes
// Include namespace system
using System;
// Csharp program for
// Check if the sum of digits of number is divisible by all of its digits
public class Divisibility
{
	public int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	public void isDigitsSumDivisible(int num)
	{
		int x = this.absValue(num);
		int sum = 0;
		Console.Write("\n Given number : " + num);
		// Sum of digit
		while (x > 0)
		{
			sum += (x % 10);
			x = x / 10;
		}
		x = this.absValue(num);
		// Check whether digit of number is divisible by sum of digits sum
		while (x > 0)
		{
			if (sum % (x % 10) != 0)
			{
				// When digit is not divisible
				Console.Write("\n No ");
				return;
			}
			x = x / 10;
		}
		// When all digit is divisible
		Console.Write("\n Yes ");
	}
	public static void Main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		// num = 24312
		// digit sum = [2 + 4 + 3 + 1 + 2] = (12)
		// 12 is divisible by 2
		// 12 is divisible by 4
		// 12 is divisible by 3
		// 12 is divisible by 1
		// 12 is divisible by 2
		// ---------------------
		// Output : Yes
		task.isDigitsSumDivisible(24312);
		// num = 21321
		// digit sum = [2+1+3+2+1] = (9)
		// 9 is not divisible by 2
		// ---------------------
		// Output : No
		task.isDigitsSumDivisible(21321);
		// num = 1221
		// digit sum = [1+2+2+1] = (6)
		// 6 is divisible by 1
		// 6 is divisible by 2
		// 6 is divisible by 2
		// 6 is divisible by 1
		// ---------------------
		// Output : Yes
		task.isDigitsSumDivisible(1221);
	}
}

Output

 Given number : 24312
 Yes
 Given number : 21321
 No
 Given number : 1221
 Yes
<?php
// Php program for
// Check if the sum of digits of number is divisible by all of its digits
class Divisibility
{
	public	function absValue($num)
	{
		if ($num < 0)
		{
			return -$num;
		}
		return $num;
	}
	public	function isDigitsSumDivisible($num)
	{
		$x = $this->absValue($num);
		$sum = 0;
		echo("\n Given number : ".$num);
		// Sum of digit
		while ($x > 0)
		{
			$sum += ($x % 10);
			$x = (int)($x / 10);
		}
		$x = $this->absValue($num);
		// Check whether digit of number is divisible by sum of digits sum
		while ($x > 0)
		{
			if ($sum % ($x % 10) != 0)
			{
				// When digit is not divisible
				echo("\n No ");
				return;
			}
			$x = (int)($x / 10);
		}
		// When all digit is divisible
		echo("\n Yes ");
	}
}

function main()
{
	$task = new Divisibility();
	// Test
	// num = 24312
	// digit sum = [2 + 4 + 3 + 1 + 2] = (12)
	// 12 is divisible by 2
	// 12 is divisible by 4
	// 12 is divisible by 3
	// 12 is divisible by 1
	// 12 is divisible by 2
	// ---------------------
	// Output : Yes
	$task->isDigitsSumDivisible(24312);
	// num = 21321
	// digit sum = [2+1+3+2+1] = (9)
	// 9 is not divisible by 2
	// ---------------------
	// Output : No
	$task->isDigitsSumDivisible(21321);
	// num = 1221
	// digit sum = [1+2+2+1] = (6)
	// 6 is divisible by 1
	// 6 is divisible by 2
	// 6 is divisible by 2
	// 6 is divisible by 1
	// ---------------------
	// Output : Yes
	$task->isDigitsSumDivisible(1221);
}
main();

Output

 Given number : 24312
 Yes
 Given number : 21321
 No
 Given number : 1221
 Yes
// Node JS program for
// Check if the sum of digits of number is divisible by all of its digits
class Divisibility
{
	absValue(num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	isDigitsSumDivisible(num)
	{
		var x = this.absValue(num);
		var sum = 0;
		process.stdout.write("\n Given number : " + num);
		// Sum of digit
		while (x > 0)
		{
			sum += (x % 10);
			x = parseInt(x / 10);
		}
		x = this.absValue(num);
		// Check whether digit of number is divisible by sum of digits sum
		while (x > 0)
		{
			if (sum % (x % 10) != 0)
			{
				// When digit is not divisible
				process.stdout.write("\n No ");
				return;
			}
			x = parseInt(x / 10);
		}
		// When all digit is divisible
		process.stdout.write("\n Yes ");
	}
}

function main()
{
	var task = new Divisibility();
	// Test
	// num = 24312
	// digit sum = [2 + 4 + 3 + 1 + 2] = (12)
	// 12 is divisible by 2
	// 12 is divisible by 4
	// 12 is divisible by 3
	// 12 is divisible by 1
	// 12 is divisible by 2
	// ---------------------
	// Output : Yes
	task.isDigitsSumDivisible(24312);
	// num = 21321
	// digit sum = [2+1+3+2+1] = (9)
	// 9 is not divisible by 2
	// ---------------------
	// Output : No
	task.isDigitsSumDivisible(21321);
	// num = 1221
	// digit sum = [1+2+2+1] = (6)
	// 6 is divisible by 1
	// 6 is divisible by 2
	// 6 is divisible by 2
	// 6 is divisible by 1
	// ---------------------
	// Output : Yes
	task.isDigitsSumDivisible(1221);
}
main();

Output

 Given number : 24312
 Yes
 Given number : 21321
 No
 Given number : 1221
 Yes
#  Python 3 program for
#  Check if the sum of digits of number is divisible by all of its digits
class Divisibility :
	def absValue(self, num) :
		if (num < 0) :
			return -num
		
		return num
	
	def isDigitsSumDivisible(self, num) :
		x = self.absValue(num)
		sum = 0
		print("\n Given number : ", num, end = "")
		#  Sum of digit
		while (x > 0) :
			sum += (x % 10)
			x = int(x / 10)
		
		x = self.absValue(num)
		#  Check whether digit of number is divisible by sum of digits sum
		while (x > 0) :
			if (sum % (x % 10) != 0) :
				#  When digit is not divisible
				print("\n No ", end = "")
				return
			
			x = int(x / 10)
		
		#  When all digit is divisible
		print("\n Yes ", end = "")
	

def main() :
	task = Divisibility()
	#  Test
	#  num = 24312
	#  digit sum = [2 + 4 + 3 + 1 + 2] = (12)
	#  12 is divisible by 2
	#  12 is divisible by 4
	#  12 is divisible by 3
	#  12 is divisible by 1
	#  12 is divisible by 2
	#  ---------------------
	#  Output : Yes
	task.isDigitsSumDivisible(24312)
	#  num = 21321
	#  digit sum = [2+1+3+2+1] = (9)
	#  9 is not divisible by 2
	#  ---------------------
	#  Output : No
	task.isDigitsSumDivisible(21321)
	#  num = 1221
	#  digit sum = [1+2+2+1] = (6)
	#  6 is divisible by 1
	#  6 is divisible by 2
	#  6 is divisible by 2
	#  6 is divisible by 1
	#  ---------------------
	#  Output : Yes
	task.isDigitsSumDivisible(1221)

if __name__ == "__main__": main()

Output

 Given number :  24312
 Yes
 Given number :  21321
 No
 Given number :  1221
 Yes
#  Ruby program for
#  Check if the sum of digits of number is divisible by all of its digits
class Divisibility 
	def absValue(num) 
		if (num < 0) 
			return -num
		end

		return num
	end

	def isDigitsSumDivisible(num) 
		x = self.absValue(num)
		sum = 0
		print("\n Given number : ", num)
		#  Sum of digit
		while (x > 0) 
			sum += (x % 10)
			x = x / 10
		end

		x = self.absValue(num)
		#  Check whether digit of number is divisible by sum of digits sum
		while (x > 0) 
			if (sum % (x % 10) != 0) 
				#  When digit is not divisible
				print("\n No ")
				return
			end

			x = x / 10
		end

		#  When all digit is divisible
		print("\n Yes ")
	end

end

def main() 
	task = Divisibility.new()
	#  Test
	#  num = 24312
	#  digit sum = [2 + 4 + 3 + 1 + 2] = (12)
	#  12 is divisible by 2
	#  12 is divisible by 4
	#  12 is divisible by 3
	#  12 is divisible by 1
	#  12 is divisible by 2
	#  ---------------------
	#  Output : Yes
	task.isDigitsSumDivisible(24312)
	#  num = 21321
	#  digit sum = [2+1+3+2+1] = (9)
	#  9 is not divisible by 2
	#  ---------------------
	#  Output : No
	task.isDigitsSumDivisible(21321)
	#  num = 1221
	#  digit sum = [1+2+2+1] = (6)
	#  6 is divisible by 1
	#  6 is divisible by 2
	#  6 is divisible by 2
	#  6 is divisible by 1
	#  ---------------------
	#  Output : Yes
	task.isDigitsSumDivisible(1221)
end

main()

Output

 Given number : 24312
 Yes 
 Given number : 21321
 No 
 Given number : 1221
 Yes 
// Scala program for
// Check if the sum of digits of number is divisible by all of its digits
class Divisibility()
{
	def absValue(num: Int): Int = {
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	def isDigitsSumDivisible(num: Int): Unit = {
		var x: Int = absValue(num);
		var sum: Int = 0;
		print("\n Given number : " + num);
		// Sum of digit
		while (x > 0)
		{
			sum += (x % 10);
			x = x / 10;
		}
		x = absValue(num);
		// Check whether digit of number is divisible by sum of digits sum
		while (x > 0)
		{
			if (sum % (x % 10) != 0)
			{
				// When digit is not divisible
				print("\n No ");
				return;
			}
			x = x / 10;
		}
		// When all digit is divisible
		print("\n Yes ");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Divisibility = new Divisibility();
		// Test
		// num = 24312
		// digit sum = [2 + 4 + 3 + 1 + 2] = (12)
		// 12 is divisible by 2
		// 12 is divisible by 4
		// 12 is divisible by 3
		// 12 is divisible by 1
		// 12 is divisible by 2
		// ---------------------
		// Output : Yes
		task.isDigitsSumDivisible(24312);
		// num = 21321
		// digit sum = [2+1+3+2+1] = (9)
		// 9 is not divisible by 2
		// ---------------------
		// Output : No
		task.isDigitsSumDivisible(21321);
		// num = 1221
		// digit sum = [1+2+2+1] = (6)
		// 6 is divisible by 1
		// 6 is divisible by 2
		// 6 is divisible by 2
		// 6 is divisible by 1
		// ---------------------
		// Output : Yes
		task.isDigitsSumDivisible(1221);
	}
}

Output

 Given number : 24312
 Yes
 Given number : 21321
 No
 Given number : 1221
 Yes
// Swift 4 program for
// Check if the sum of digits of number is divisible by all of its digits
class Divisibility
{
	func absValue(_ num: Int) -> Int
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	func isDigitsSumDivisible(_ num: Int)
	{
		var x: Int = self.absValue(num);
		var sum: Int = 0;
		print("\n Given number : ", num, terminator: "");
		// Sum of digit
		while (x > 0)
		{
			sum += (x % 10);
			x = x / 10;
		}
		x = self.absValue(num);
		// Check whether digit of number is divisible by sum of digits sum
		while (x > 0)
		{
			if (sum % (x % 10)  != 0)
			{
				// When digit is not divisible
				print("\n No ", terminator: "");
				return;
			}
			x = x / 10;
		}
		// When all digit is divisible
		print("\n Yes ", terminator: "");
	}
}
func main()
{
	let task: Divisibility = Divisibility();
	// Test
	// num = 24312
	// digit sum = [2 + 4 + 3 + 1 + 2] = (12)
	// 12 is divisible by 2
	// 12 is divisible by 4
	// 12 is divisible by 3
	// 12 is divisible by 1
	// 12 is divisible by 2
	// ---------------------
	// Output : Yes
	task.isDigitsSumDivisible(24312);
	// num = 21321
	// digit sum = [2+1+3+2+1] = (9)
	// 9 is not divisible by 2
	// ---------------------
	// Output : No
	task.isDigitsSumDivisible(21321);
	// num = 1221
	// digit sum = [1+2+2+1] = (6)
	// 6 is divisible by 1
	// 6 is divisible by 2
	// 6 is divisible by 2
	// 6 is divisible by 1
	// ---------------------
	// Output : Yes
	task.isDigitsSumDivisible(1221);
}
main();

Output

 Given number :  24312
 Yes
 Given number :  21321
 No
 Given number :  1221
 Yes
// Kotlin program for
// Check if the sum of digits of number is divisible by all of its digits
class Divisibility
{
	fun absValue(num: Int): Int
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	fun isDigitsSumDivisible(num: Int): Unit
	{
		var x: Int = this.absValue(num);
		var sum: Int = 0;
		print("\n Given number : " + num);
		// Sum of digit
		while (x > 0)
		{
			sum += (x % 10);
			x = x / 10;
		}
		x = this.absValue(num);
		// Check whether digit of number is divisible by sum of digits sum
		while (x > 0)
		{
			if (sum % (x % 10) != 0)
			{
				// When digit is not divisible
				print("\n No ");
				return;
			}
			x = x / 10;
		}
		// When all digit is divisible
		print("\n Yes ");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Divisibility = Divisibility();
	// Test
	// num = 24312
	// digit sum = [2 + 4 + 3 + 1 + 2] = (12)
	// 12 is divisible by 2
	// 12 is divisible by 4
	// 12 is divisible by 3
	// 12 is divisible by 1
	// 12 is divisible by 2
	// ---------------------
	// Output : Yes
	task.isDigitsSumDivisible(24312);
	// num = 21321
	// digit sum = [2+1+3+2+1] = (9)
	// 9 is not divisible by 2
	// ---------------------
	// Output : No
	task.isDigitsSumDivisible(21321);
	// num = 1221
	// digit sum = [1+2+2+1] = (6)
	// 6 is divisible by 1
	// 6 is divisible by 2
	// 6 is divisible by 2
	// 6 is divisible by 1
	// ---------------------
	// Output : Yes
	task.isDigitsSumDivisible(1221);
}

Output

 Given number : 24312
 Yes
 Given number : 21321
 No
 Given number : 1221
 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