Check if a number is divisible by sum of its digits

Here given code implementation process.

// C Program for
// Check if a number is divisible by sum 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);
	int sum = 0;
	printf("\n Given number  : %d ", num);
	// Sum of digit
	while (x > 0)
	{
		sum += (x % 10);
		x = x / 10;
	}
	if (num % sum == 0)
	{
		printf("\n Yes ");
	}
	else
	{
		printf("\n No ");
	}
}
int main(int argc, char
	const *argv[])
{
	// Test
	// num = 24312
	// digit sum = [2 + 4 + 3 + 1 + 2] = (12)
	// 24312 % 12 = 0
	// ---------------------
	// Output : Yes
	numDivisibleByDigitSum(24312);
	// num = 21321
	// digit sum = [2+1+3+2+1] = (9)
	// 21321 is not divisible by 9
	// ---------------------
	// Output : Yes
	numDivisibleByDigitSum(21321);
	// num = 1221
	// digit sum = [1+2+2+1] = (6)
	// 1221 is not divisible by 6
	// ---------------------
	// Output : No
	numDivisibleByDigitSum(1221);
	return 0;
}

Output

 Given number  : 24312
 Yes
 Given number  : 21321
 Yes
 Given number  : 1221
 No
// Java program for
// Check if a number is divisible by sum 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);
		int sum = 0;
		System.out.print("\n Given number : " + num);
		// Sum of digit
		while (x > 0)
		{
			sum += (x % 10);
			x = x / 10;
		}
		if (num % sum == 0)
		{
			System.out.print("\n Yes ");
		}
		else
		{
			System.out.print("\n No ");
		}
	}
	public static void main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		// num = 24312
		// digit sum = [2 + 4 + 3 + 1 + 2] = (12)
		// 24312 % 12 = 0
		// ---------------------
		// Output : Yes
		task.numDivisibleByDigitSum(24312);
		// num = 21321
		// digit sum = [2+1+3+2+1] = (9)
		// 21321 is not divisible by 9
		// ---------------------
		// Output : Yes
		task.numDivisibleByDigitSum(21321);
		// num = 1221
		// digit sum = [1+2+2+1] = (6)
		// 1221 is not divisible by 6
		// ---------------------
		// Output : No
		task.numDivisibleByDigitSum(1221);
	}
}

Output

 Given number : 24312
 Yes
 Given number : 21321
 Yes
 Given number : 1221
 No
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Check if a number is divisible by sum 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);
		int sum = 0;
		cout << "\n Given number : " << num;
		// Sum of digit
		while (x > 0)
		{
			sum += (x % 10);
			x = x / 10;
		}
		if (num % sum == 0)
		{
			cout << "\n Yes ";
		}
		else
		{
			cout << "\n No ";
		}
	}
};
int main()
{
	Divisibility *task = new Divisibility();
	// Test
	// num = 24312
	// digit sum = [2 + 4 + 3 + 1 + 2] = (12)
	// 24312 % 12 = 0
	// ---------------------
	// Output : Yes
	task->numDivisibleByDigitSum(24312);
	// num = 21321
	// digit sum = [2+1+3+2+1] = (9)
	// 21321 is not divisible by 9
	// ---------------------
	// Output : Yes
	task->numDivisibleByDigitSum(21321);
	// num = 1221
	// digit sum = [1+2+2+1] = (6)
	// 1221 is not divisible by 6
	// ---------------------
	// Output : No
	task->numDivisibleByDigitSum(1221);
	return 0;
}

Output

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

Output

 Given number : 24312
 Yes
 Given number : 21321
 Yes
 Given number : 1221
 No
// Include namespace system
using System;
// Csharp program for
// Check if a number is divisible by sum 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);
		int sum = 0;
		Console.Write("\n Given number : " + num);
		// Sum of digit
		while (x > 0)
		{
			sum += (x % 10);
			x = x / 10;
		}
		if (num % sum == 0)
		{
			Console.Write("\n Yes ");
		}
		else
		{
			Console.Write("\n No ");
		}
	}
	public static void Main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		// num = 24312
		// digit sum = [2 + 4 + 3 + 1 + 2] = (12)
		// 24312 % 12 = 0
		// ---------------------
		// Output : Yes
		task.numDivisibleByDigitSum(24312);
		// num = 21321
		// digit sum = [2+1+3+2+1] = (9)
		// 21321 is not divisible by 9
		// ---------------------
		// Output : Yes
		task.numDivisibleByDigitSum(21321);
		// num = 1221
		// digit sum = [1+2+2+1] = (6)
		// 1221 is not divisible by 6
		// ---------------------
		// Output : No
		task.numDivisibleByDigitSum(1221);
	}
}

Output

 Given number : 24312
 Yes
 Given number : 21321
 Yes
 Given number : 1221
 No
<?php
// Php program for
// Check if a number is divisible by sum of its digits
class Divisibility
{
	public	function absValue($num)
	{
		if ($num < 0)
		{
			return -$num;
		}
		return $num;
	}
	public	function numDivisibleByDigitSum($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);
		}
		if ($num % $sum == 0)
		{
			echo("\n Yes ");
		}
		else
		{
			echo("\n No ");
		}
	}
}

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

Output

 Given number : 24312
 Yes
 Given number : 21321
 Yes
 Given number : 1221
 No
// Node JS program for
// Check if a number is divisible by sum of its digits
class Divisibility
{
	absValue(num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	numDivisibleByDigitSum(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);
		}
		if (num % sum == 0)
		{
			process.stdout.write("\n Yes ");
		}
		else
		{
			process.stdout.write("\n No ");
		}
	}
}

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

Output

 Given number : 24312
 Yes
 Given number : 21321
 Yes
 Given number : 1221
 No
#  Python 3 program for
#  Check if a number is divisible by sum of its digits
class Divisibility :
	def absValue(self, num) :
		if (num < 0) :
			return -num
		
		return num
	
	def numDivisibleByDigitSum(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)
		
		if (num % sum == 0) :
			print("\n Yes ", end = "")
		else :
			print("\n No ", end = "")
		
	

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

if __name__ == "__main__": main()

Output

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

		return num
	end

	def numDivisibleByDigitSum(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

		if (num % sum == 0) 
			print("\n Yes ")
		else
 
			print("\n No ")
		end

	end

end

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

main()

Output

 Given number : 24312
 Yes 
 Given number : 21321
 Yes 
 Given number : 1221
 No 
// Scala program for
// Check if a number is divisible by sum 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);
		var sum: Int = 0;
		print("\n Given number : " + num);
		// Sum of digit
		while (x > 0)
		{
			sum += (x % 10);
			x = x / 10;
		}
		if (num % sum == 0)
		{
			print("\n Yes ");
		}
		else
		{
			print("\n No ");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Divisibility = new Divisibility();
		// Test
		// num = 24312
		// digit sum = [2 + 4 + 3 + 1 + 2] = (12)
		// 24312 % 12 = 0
		// ---------------------
		// Output : Yes
		task.numDivisibleByDigitSum(24312);
		// num = 21321
		// digit sum = [2+1+3+2+1] = (9)
		// 21321 is not divisible by 9
		// ---------------------
		// Output : Yes
		task.numDivisibleByDigitSum(21321);
		// num = 1221
		// digit sum = [1+2+2+1] = (6)
		// 1221 is not divisible by 6
		// ---------------------
		// Output : No
		task.numDivisibleByDigitSum(1221);
	}
}

Output

 Given number : 24312
 Yes
 Given number : 21321
 Yes
 Given number : 1221
 No
// Swift 4 program for
// Check if a number is divisible by sum 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);
		var sum: Int = 0;
		print("\n Given number : ", num, terminator: "");
		// Sum of digit
		while (x > 0)
		{
			sum += (x % 10);
			x = x / 10;
		}
		if (num % sum == 0)
		{
			print("\n Yes ", terminator: "");
		}
		else
		{
			print("\n No ", terminator: "");
		}
	}
}
func main()
{
	let task: Divisibility = Divisibility();
	// Test
	// num = 24312
	// digit sum = [2 + 4 + 3 + 1 + 2] = (12)
	// 24312 % 12 = 0
	// ---------------------
	// Output : Yes
	task.numDivisibleByDigitSum(24312);
	// num = 21321
	// digit sum = [2+1+3+2+1] = (9)
	// 21321 is not divisible by 9
	// ---------------------
	// Output : Yes
	task.numDivisibleByDigitSum(21321);
	// num = 1221
	// digit sum = [1+2+2+1] = (6)
	// 1221 is not divisible by 6
	// ---------------------
	// Output : No
	task.numDivisibleByDigitSum(1221);
}
main();

Output

 Given number :  24312
 Yes
 Given number :  21321
 Yes
 Given number :  1221
 No
// Kotlin program for
// Check if a number is divisible by sum 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);
		var sum: Int = 0;
		print("\n Given number : " + num);
		// Sum of digit
		while (x > 0)
		{
			sum += (x % 10);
			x = x / 10;
		}
		if (num % sum == 0)
		{
			print("\n Yes ");
		}
		else
		{
			print("\n No ");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Divisibility = Divisibility();
	// Test
	// num = 24312
	// digit sum = [2 + 4 + 3 + 1 + 2] = (12)
	// 24312 % 12 = 0
	// ---------------------
	// Output : Yes
	task.numDivisibleByDigitSum(24312);
	// num = 21321
	// digit sum = [2+1+3+2+1] = (9)
	// 21321 is not divisible by 9
	// ---------------------
	// Output : Yes
	task.numDivisibleByDigitSum(21321);
	// num = 1221
	// digit sum = [1+2+2+1] = (6)
	// 1221 is not divisible by 6
	// ---------------------
	// Output : No
	task.numDivisibleByDigitSum(1221);
}

Output

 Given number : 24312
 Yes
 Given number : 21321
 Yes
 Given number : 1221
 No


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