Check whether sum of digits at odd places of a number is divisible by K

Here given code implementation process.

// C Program for
// Check whether sum of digits at odd places of a number is divisible by K
#include <stdio.h>

int absValue(int num)
{
	if (num < 0)
	{
		return -num;
	}
	return num;
}
void oddPlaceSumDividByK(int num, int k)
{
	printf("\n Given number  : %d ", num);
	printf("\n Given k  : %d ", k);
	int sum = 0;
	int selector = 1;
	int x = absValue(num);
	// Sum of digit
	while (x > 0)
	{
		if (selector == 1)
		{
			// Odd place number
			sum += (x % 10);
			selector = 0;
		}
		else
		{
			selector = 1;
		}
		x = x / 10;
	}
	if ((sum % k) != 0)
	{
		printf("\n (%d %% %d) : No ", sum, k);
	}
	else
	{
		printf("\n (%d %% %d) : Yes ", sum, k);
	}
}
int main(int argc, char
	const *argv[])
{
	// Test
	// num = 54273 k = 5
	// Odd place digit sum
	// [5 + 2 + 3] = 10
	//  (10 % 5) == 0
	// ---------------------
	// Output : Yes 
	oddPlaceSumDividByK(54273, 5);
	// num = 423
	//   k = 6
	// Odd place digit sum
	// [4 + 3] = 7
	// (7 % 6) != 0
	// ---------------------
	// Output : No
	oddPlaceSumDividByK(423, 6);
	// num = 24
	//   k = 2
	// Odd place digit sum
	//   [4] = 4
	// (4 % 2) == 0
	// ---------------------
	// Output : Yes
	oddPlaceSumDividByK(24, 2);
	return 0;
}

Output

 Given number  : 54273
 Given k  : 5
 (10 % 5) : Yes
 Given number  : 423
 Given k  : 6
 (7 % 6) : No
 Given number  : 24
 Given k  : 2
 (4 % 2) : Yes
// Java program for
// Check whether sum of digits at odd places of a number is divisible by K
public class Divisibility
{
	public int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	public void oddPlaceSumDividByK(int num, int k)
	{
		System.out.print("\n Given number : " + num);
		System.out.print("\n Given k : " + k);
		int sum = 0;
		int selector = 1;
		int x = absValue(num);
		// Sum of digit
		while (x > 0)
		{
			if (selector == 1)
			{
				// Odd place number
				sum += (x % 10);
				selector = 0;
			}
			else
			{
				selector = 1;
			}
			x = x / 10;
		}
		if ((sum % k) != 0)
		{
			System.out.print("\n (" + sum + " % " + k + ") : No ");
		}
		else
		{
			System.out.print("\n (" + sum + " % " + k + ") : Yes ");
		}
	}
	public static void main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		// num = 54273 k = 5
		// Odd place digit sum
		// [5 + 2 + 3] = 10
		//  (10 % 5) == 0
		// ---------------------
		// Output : Yes 
		task.oddPlaceSumDividByK(54273, 5);
		// num = 423
		//   k = 6
		// Odd place digit sum
		// [4 + 3] = 7
		// (7 % 6) != 0
		// ---------------------
		// Output : No
		task.oddPlaceSumDividByK(423, 6);
		// num = 24
		//   k = 2
		// Odd place digit sum
		//   [4] = 4
		// (4 % 2) == 0
		// ---------------------
		// Output : Yes
		task.oddPlaceSumDividByK(24, 2);
	}
}

Output

 Given number : 54273
 Given k : 5
 (10 % 5) : Yes
 Given number : 423
 Given k : 6
 (7 % 6) : No
 Given number : 24
 Given k : 2
 (4 % 2) : Yes
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Check whether sum of digits at odd places of a number is divisible by K
class Divisibility
{
	public: int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	void oddPlaceSumDividByK(int num, int k)
	{
		cout << "\n Given number : " << num;
		cout << "\n Given k : " << k;
		int sum = 0;
		int selector = 1;
		int x = this->absValue(num);
		// Sum of digit
		while (x > 0)
		{
			if (selector == 1)
			{
				// Odd place number
				sum += (x % 10);
				selector = 0;
			}
			else
			{
				selector = 1;
			}
			x = x / 10;
		}
		if ((sum % k) != 0)
		{
			cout << "\n (" << sum << " % " << k << ") : No ";
		}
		else
		{
			cout << "\n (" << sum << " % " << k << ") : Yes ";
		}
	}
};
int main()
{
	Divisibility *task = new Divisibility();
	// Test
	// num = 54273 k = 5
	// Odd place digit sum
	// [5 + 2 + 3] = 10
	//  (10 % 5) == 0
	// ---------------------
	// Output : Yes 
	task->oddPlaceSumDividByK(54273, 5);
	// num = 423
	//   k = 6
	// Odd place digit sum
	// [4 + 3] = 7
	// (7 % 6) != 0
	// ---------------------
	// Output : No
	task->oddPlaceSumDividByK(423, 6);
	// num = 24
	//   k = 2
	// Odd place digit sum
	//   [4] = 4
	// (4 % 2) == 0
	// ---------------------
	// Output : Yes
	task->oddPlaceSumDividByK(24, 2);
	return 0;
}

Output

 Given number : 54273
 Given k : 5
 (10 % 5) : Yes
 Given number : 423
 Given k : 6
 (7 % 6) : No
 Given number : 24
 Given k : 2
 (4 % 2) : Yes
package main
import "fmt"
// Go program for
// Check whether sum of digits at odd places of a number is divisible by K
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) oddPlaceSumDividByK(num, k int) {
	fmt.Print("\n Given number : ", num)
	fmt.Print("\n Given k : ", k)
	var sum int = 0
	var selector int = 1
	var x int = this.absValue(num)
	// Sum of digit
	for (x > 0) {
		if selector == 1 {
			// Odd place number
			sum += (x % 10)
			selector = 0
		} else {
			selector = 1
		}
		x = x / 10
	}
	if (sum % k) != 0 {
		fmt.Print("\n (", sum, " % ", k, ") : No ")
	} else {
		fmt.Print("\n (", sum, " % ", k, ") : Yes ")
	}
}
func main() {
	var task * Divisibility = getDivisibility()
	// Test
	// num = 54273 k = 5
	// Odd place digit sum
	// [5 + 2 + 3] = 10
	//  (10 % 5) == 0
	// ---------------------
	// Output : Yes 
	task.oddPlaceSumDividByK(54273, 5)
	// num = 423
	//   k = 6
	// Odd place digit sum
	// [4 + 3] = 7
	// (7 % 6) != 0
	// ---------------------
	// Output : No
	task.oddPlaceSumDividByK(423, 6)
	// num = 24
	//   k = 2
	// Odd place digit sum
	//   [4] = 4
	// (4 % 2) == 0
	// ---------------------
	// Output : Yes
	task.oddPlaceSumDividByK(24, 2)
}

Output

 Given number : 54273
 Given k : 5
 (10 % 5) : Yes
 Given number : 423
 Given k : 6
 (7 % 6) : No
 Given number : 24
 Given k : 2
 (4 % 2) : Yes
// Include namespace system
using System;
// Csharp program for
// Check whether sum of digits at odd places of a number is divisible by K
public class Divisibility
{
	public int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	public void oddPlaceSumDividByK(int num, int k)
	{
		Console.Write("\n Given number : " + num);
		Console.Write("\n Given k : " + k);
		int sum = 0;
		int selector = 1;
		int x = this.absValue(num);
		// Sum of digit
		while (x > 0)
		{
			if (selector == 1)
			{
				// Odd place number
				sum += (x % 10);
				selector = 0;
			}
			else
			{
				selector = 1;
			}
			x = x / 10;
		}
		if ((sum % k) != 0)
		{
			Console.Write("\n (" + sum + " % " + k + ") : No ");
		}
		else
		{
			Console.Write("\n (" + sum + " % " + k + ") : Yes ");
		}
	}
	public static void Main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		// num = 54273 k = 5
		// Odd place digit sum
		// [5 + 2 + 3] = 10
		//  (10 % 5) == 0
		// ---------------------
		// Output : Yes 
		task.oddPlaceSumDividByK(54273, 5);
		// num = 423
		//   k = 6
		// Odd place digit sum
		// [4 + 3] = 7
		// (7 % 6) != 0
		// ---------------------
		// Output : No
		task.oddPlaceSumDividByK(423, 6);
		// num = 24
		//   k = 2
		// Odd place digit sum
		//   [4] = 4
		// (4 % 2) == 0
		// ---------------------
		// Output : Yes
		task.oddPlaceSumDividByK(24, 2);
	}
}

Output

 Given number : 54273
 Given k : 5
 (10 % 5) : Yes
 Given number : 423
 Given k : 6
 (7 % 6) : No
 Given number : 24
 Given k : 2
 (4 % 2) : Yes
<?php
// Php program for
// Check whether sum of digits at odd places of a number is divisible by K
class Divisibility
{
	public	function absValue($num)
	{
		if ($num < 0)
		{
			return -$num;
		}
		return $num;
	}
	public	function oddPlaceSumDividByK($num, $k)
	{
		echo("\n Given number : ".$num);
		echo("\n Given k : ".$k);
		$sum = 0;
		$selector = 1;
		$x = $this->absValue($num);
		// Sum of digit
		while ($x > 0)
		{
			if ($selector == 1)
			{
				// Odd place number
				$sum += ($x % 10);
				$selector = 0;
			}
			else
			{
				$selector = 1;
			}
			$x = (int)($x / 10);
		}
		if (($sum % $k) != 0)
		{
			echo("\n (".$sum.
				" % ".$k.
				") : No ");
		}
		else
		{
			echo("\n (".$sum.
				" % ".$k.
				") : Yes ");
		}
	}
}

function main()
{
	$task = new Divisibility();
	// Test
	// num = 54273 k = 5
	// Odd place digit sum
	// [5 + 2 + 3] = 10
	//  (10 % 5) == 0
	// ---------------------
	// Output : Yes 
	$task->oddPlaceSumDividByK(54273, 5);
	// num = 423
	//   k = 6
	// Odd place digit sum
	// [4 + 3] = 7
	// (7 % 6) != 0
	// ---------------------
	// Output : No
	$task->oddPlaceSumDividByK(423, 6);
	// num = 24
	//   k = 2
	// Odd place digit sum
	//   [4] = 4
	// (4 % 2) == 0
	// ---------------------
	// Output : Yes
	$task->oddPlaceSumDividByK(24, 2);
}
main();

Output

 Given number : 54273
 Given k : 5
 (10 % 5) : Yes
 Given number : 423
 Given k : 6
 (7 % 6) : No
 Given number : 24
 Given k : 2
 (4 % 2) : Yes
// Node JS program for
// Check whether sum of digits at odd places of a number is divisible by K
class Divisibility
{
	absValue(num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	oddPlaceSumDividByK(num, k)
	{
		process.stdout.write("\n Given number : " + num);
		process.stdout.write("\n Given k : " + k);
		var sum = 0;
		var selector = 1;
		var x = this.absValue(num);
		// Sum of digit
		while (x > 0)
		{
			if (selector == 1)
			{
				// Odd place number
				sum += (x % 10);
				selector = 0;
			}
			else
			{
				selector = 1;
			}
			x = parseInt(x / 10);
		}
		if ((sum % k) != 0)
		{
			process.stdout.write("\n (" + sum + " % " + k + ") : No ");
		}
		else
		{
			process.stdout.write("\n (" + sum + " % " + k + ") : Yes ");
		}
	}
}

function main()
{
	var task = new Divisibility();
	// Test
	// num = 54273 k = 5
	// Odd place digit sum
	// [5 + 2 + 3] = 10
	//  (10 % 5) == 0
	// ---------------------
	// Output : Yes 
	task.oddPlaceSumDividByK(54273, 5);
	// num = 423
	//   k = 6
	// Odd place digit sum
	// [4 + 3] = 7
	// (7 % 6) != 0
	// ---------------------
	// Output : No
	task.oddPlaceSumDividByK(423, 6);
	// num = 24
	//   k = 2
	// Odd place digit sum
	//   [4] = 4
	// (4 % 2) == 0
	// ---------------------
	// Output : Yes
	task.oddPlaceSumDividByK(24, 2);
}
main();

Output

 Given number : 54273
 Given k : 5
 (10 % 5) : Yes
 Given number : 423
 Given k : 6
 (7 % 6) : No
 Given number : 24
 Given k : 2
 (4 % 2) : Yes
#  Python 3 program for
#  Check whether sum of digits at odd places of a number is divisible by K
class Divisibility :
	def absValue(self, num) :
		if (num < 0) :
			return -num
		
		return num
	
	def oddPlaceSumDividByK(self, num, k) :
		print("\n Given number : ", num, end = "")
		print("\n Given k : ", k, end = "")
		sum = 0
		selector = 1
		x = self.absValue(num)
		#  Sum of digit
		while (x > 0) :
			if (selector == 1) :
				#  Odd place number
				sum += (x % 10)
				selector = 0
			else :
				selector = 1
			
			x = int(x / 10)
		
		if ((sum % k) != 0) :
			print("\n (", sum ," % ", k ,") : No ", end = "")
		else :
			print("\n (", sum ," % ", k ,") : Yes ", end = "")
		
	

def main() :
	task = Divisibility()
	#  Test
	#  num = 54273 k = 5
	#  Odd place digit sum
	#  [5 + 2 + 3] = 10
	#   (10 % 5) == 0
	#  ---------------------
	#  Output : Yes 
	task.oddPlaceSumDividByK(54273, 5)
	#  num = 423
	#    k = 6
	#  Odd place digit sum
	#  [4 + 3] = 7
	#  (7 % 6) != 0
	#  ---------------------
	#  Output : No
	task.oddPlaceSumDividByK(423, 6)
	#  num = 24
	#    k = 2
	#  Odd place digit sum
	#    [4] = 4
	#  (4 % 2) == 0
	#  ---------------------
	#  Output : Yes
	task.oddPlaceSumDividByK(24, 2)

if __name__ == "__main__": main()

Output

 Given number :  54273
 Given k :  5
 ( 10  %  5 ) : Yes
 Given number :  423
 Given k :  6
 ( 7  %  6 ) : No
 Given number :  24
 Given k :  2
 ( 4  %  2 ) : Yes
#  Ruby program for
#  Check whether sum of digits at odd places of a number is divisible by K
class Divisibility 
	def absValue(num) 
		if (num < 0) 
			return -num
		end

		return num
	end

	def oddPlaceSumDividByK(num, k) 
		print("\n Given number : ", num)
		print("\n Given k : ", k)
		sum = 0
		selector = 1
		x = self.absValue(num)
		#  Sum of digit
		while (x > 0) 
			if (selector == 1) 
				#  Odd place number
				sum += (x % 10)
				selector = 0
			else
 
				selector = 1
			end

			x = x / 10
		end

		if ((sum % k) != 0) 
			print("\n (", sum ," % ", k ,") : No ")
		else
 
			print("\n (", sum ," % ", k ,") : Yes ")
		end

	end

end

def main() 
	task = Divisibility.new()
	#  Test
	#  num = 54273 k = 5
	#  Odd place digit sum
	#  [5 + 2 + 3] = 10
	#   (10 % 5) == 0
	#  ---------------------
	#  Output : Yes 
	task.oddPlaceSumDividByK(54273, 5)
	#  num = 423
	#    k = 6
	#  Odd place digit sum
	#  [4 + 3] = 7
	#  (7 % 6) != 0
	#  ---------------------
	#  Output : No
	task.oddPlaceSumDividByK(423, 6)
	#  num = 24
	#    k = 2
	#  Odd place digit sum
	#    [4] = 4
	#  (4 % 2) == 0
	#  ---------------------
	#  Output : Yes
	task.oddPlaceSumDividByK(24, 2)
end

main()

Output

 Given number : 54273
 Given k : 5
 (10 % 5) : Yes 
 Given number : 423
 Given k : 6
 (7 % 6) : No 
 Given number : 24
 Given k : 2
 (4 % 2) : Yes 
// Scala program for
// Check whether sum of digits at odd places of a number is divisible by K
class Divisibility()
{
	def absValue(num: Int): Int = {
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	def oddPlaceSumDividByK(num: Int, k: Int): Unit = {
		print("\n Given number : " + num);
		print("\n Given k : " + k);
		var sum: Int = 0;
		var selector: Int = 1;
		var x: Int = absValue(num);
		// Sum of digit
		while (x > 0)
		{
			if (selector == 1)
			{
				// Odd place number
				sum += (x % 10);
				selector = 0;
			}
			else
			{
				selector = 1;
			}
			x = x / 10;
		}
		if ((sum % k) != 0)
		{
			print("\n (" + sum + " % " + k + ") : No ");
		}
		else
		{
			print("\n (" + sum + " % " + k + ") : Yes ");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Divisibility = new Divisibility();
		// Test
		// num = 54273 k = 5
		// Odd place digit sum
		// [5 + 2 + 3] = 10
		//  (10 % 5) == 0
		// ---------------------
		// Output : Yes 
		task.oddPlaceSumDividByK(54273, 5);
		// num = 423
		//   k = 6
		// Odd place digit sum
		// [4 + 3] = 7
		// (7 % 6) != 0
		// ---------------------
		// Output : No
		task.oddPlaceSumDividByK(423, 6);
		// num = 24
		//   k = 2
		// Odd place digit sum
		//   [4] = 4
		// (4 % 2) == 0
		// ---------------------
		// Output : Yes
		task.oddPlaceSumDividByK(24, 2);
	}
}

Output

 Given number : 54273
 Given k : 5
 (10 % 5) : Yes
 Given number : 423
 Given k : 6
 (7 % 6) : No
 Given number : 24
 Given k : 2
 (4 % 2) : Yes
// Swift 4 program for
// Check whether sum of digits at odd places of a number is divisible by K
class Divisibility
{
	func absValue(_ num: Int) -> Int
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	func oddPlaceSumDividByK(_ num: Int, _ k: Int)
	{
		print("\n Given number : ", num, terminator: "");
		print("\n Given k : ", k, terminator: "");
		var sum: Int = 0;
		var selector: Int = 1;
		var x: Int = self.absValue(num);
		// Sum of digit
		while (x > 0)
		{
			if (selector == 1)
			{
				// Odd place number
				sum += (x % 10);
				selector = 0;
			}
			else
			{
				selector = 1;
			}
			x = x / 10;
		}
		if ((sum % k)  != 0)
		{
			print("\n (", sum ," % ", k ,") : No ", terminator: "");
		}
		else
		{
			print("\n (", sum ," % ", k ,") : Yes ", terminator: "");
		}
	}
}
func main()
{
	let task: Divisibility = Divisibility();
	// Test
	// num = 54273 k = 5
	// Odd place digit sum
	// [5 + 2 + 3] = 10
	//  (10 % 5) == 0
	// ---------------------
	// Output : Yes 
	task.oddPlaceSumDividByK(54273, 5);
	// num = 423
	//   k = 6
	// Odd place digit sum
	// [4 + 3] = 7
	// (7 % 6)  != 0
	// ---------------------
	// Output : No
	task.oddPlaceSumDividByK(423, 6);
	// num = 24
	//   k = 2
	// Odd place digit sum
	//   [4] = 4
	// (4 % 2) == 0
	// ---------------------
	// Output : Yes
	task.oddPlaceSumDividByK(24, 2);
}
main();

Output

 Given number :  54273
 Given k :  5
 ( 10  %  5 ) : Yes
 Given number :  423
 Given k :  6
 ( 7  %  6 ) : No
 Given number :  24
 Given k :  2
 ( 4  %  2 ) : Yes
// Kotlin program for
// Check whether sum of digits at odd places of a number is divisible by K
class Divisibility
{
	fun absValue(num: Int): Int
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	fun oddPlaceSumDividByK(num: Int, k: Int): Unit
	{
		print("\n Given number : " + num);
		print("\n Given k : " + k);
		var sum: Int = 0;
		var selector: Int = 1;
		var x: Int = this.absValue(num);
		// Sum of digit
		while (x > 0)
		{
			if (selector == 1)
			{
				// Odd place number
				sum += (x % 10);
				selector = 0;
			}
			else
			{
				selector = 1;
			}
			x = x / 10;
		}
		if ((sum % k) != 0)
		{
			print("\n (" + sum + " % " + k + ") : No ");
		}
		else
		{
			print("\n (" + sum + " % " + k + ") : Yes ");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Divisibility = Divisibility();
	// Test
	// num = 54273 k = 5
	// Odd place digit sum
	// [5 + 2 + 3] = 10
	//  (10 % 5) == 0
	// ---------------------
	// Output : Yes 
	task.oddPlaceSumDividByK(54273, 5);
	// num = 423
	//   k = 6
	// Odd place digit sum
	// [4 + 3] = 7
	// (7 % 6) != 0
	// ---------------------
	// Output : No
	task.oddPlaceSumDividByK(423, 6);
	// num = 24
	//   k = 2
	// Odd place digit sum
	//   [4] = 4
	// (4 % 2) == 0
	// ---------------------
	// Output : Yes
	task.oddPlaceSumDividByK(24, 2);
}

Output

 Given number : 54273
 Given k : 5
 (10 % 5) : Yes
 Given number : 423
 Given k : 6
 (7 % 6) : No
 Given number : 24
 Given k : 2
 (4 % 2) : 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