Skip to main content

Display all possible permutations of a number divisible by K

Here given code implementation process.

// C Program
// Display all possible permutations of a number divisible by K
#include <stdio.h>
#include <stdlib.h> // for atoi
#include <string.h> // strlen

// Swap two elements in given string
// i and j is location
void swap(char str[], int i, int j)
{
	char temp = str[i];
	str[i] = str[j];
	str[j] = temp;
}
void permutation(char str[], int index, int k, int n)
{
	if (index == n)
	{
      	// We consider n digit result.
        // So decimal numbers are not start to zero.
		if (str[0] != '0' && index > 1)
		{
			if (atoi(str) % k == 0)
			{   
             
				printf("\n %s", str);
			}
		}
		return;
	}
	for (int i = index; i < n; ++i)
	{
		//swap the array element
		swap(str, i, index);
		permutation(str, index + 1, k, n);
		//swap the array element
		swap(str, i, index);
	}
}
void divisiblePermutation(char num[], int k)
{
	// Get the size 
	int n = strlen(num);
	printf("\n Given num : %s", num);
	printf("\n Display k : %d", k);
	permutation(num, 0, k, n);
	printf("\n");
}
int main()
{
	// Given number1
	char num1[] = "5032";
	int k = 8;
	divisiblePermutation(num1, k);
	// Given number2
	char num2[] = "45261";
	k = 7;
	divisiblePermutation(num2, k);
	return 0;
}

Output

 Given num : 5032
 Display k : 8
 5032
 5320
 3520

 Given num : 45261
 Display k : 7
 45612
 42651
 41265
 54621
 52416
 52164
 25641
 26145
 21546
 65142
 64512
 61425
 15246
 15624
 16254
/*
    Java Program
    Display all possible permutations of a number divisible by K
*/
public class Divisibility
{
	// Swap two elements in given string
	// i and j is location
	public void swap(char[] str, int i, int j)
	{
		char temp = str[i];
		str[i] = str[j];
		str[j] = temp;
	}
	public void permutation(
      char[] str, int index, 
      int k, int n, String output)
	{
		if (index == n)
		{
			// We consider n digit result.
			// So decimal numbers are not start to zero.
			if (output.length() != 0 && output.charAt(0) != '0' && index > 1)
			{
				if (Integer.parseInt(output) % k == 0)
				{
					System.out.print("\n " + output);
				}
			}
			return;
		}
		for (int i = index; i < n; ++i)
		{
			//swap the array element
			swap(str, i, index);
			permutation(str, index + 1, k, n, output + str[index]);
			//swap the array element
			swap(str, i, index);
		}
	}
	public void divisiblePermutation(int num, int k)
	{
		// Converting int num to string
		String text = "" + num;
		// Convert a string into char array
		char[] str = text.toCharArray();
		// Get the size 
		int n = text.length();
		System.out.print("\n Given num : " + num);
		System.out.print("\n Display k : " + k);
		// Find permutation which is divisible by k
		permutation(str, 0, k, n, "");
		System.out.print("\n");
	}
	public static void main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Given number1
		int num = 5032;
		int k = 8;
		task.divisiblePermutation(num, k);
		// Given number2
		num = 45261;
		k = 7;
		task.divisiblePermutation(num, k);
	}
}

Output

 Given num : 5032
 Display k : 8
 5032
 5320
 3520

 Given num : 45261
 Display k : 7
 45612
 42651
 41265
 54621
 52416
 52164
 25641
 26145
 21546
 65142
 64512
 61425
 15246
 15624
 16254
// Include header file
#include <iostream>
#include <string>
using namespace std;
/*
    C++ Program
    Display all possible permutations of a number divisible by K
*/
class Divisibility
{
	public:
		// Swap two elements in given string
		// i and j is location
		string swap(string str, int i, int j)
		{
			char temp = str[i];
			str[i] = str[j];
			str[j] = temp;
            return str;
		}
	void permutation(string str, int index, int k, int n, string output)
	{
		if (index == n)
		{
			// We consider n digit result.
			// So decimal numbers are not start to zero.
			if (output.length() != 0 && output[0] != '0' && index > 1)
			{
				if (stoi(output) % k == 0)
				{
					cout << "\n " << output;
				}
			}
			return;
		}
		for (int i = index; i < n; ++i)
		{
			//swap the array element
			str = this->swap(str, i, index);
			this->permutation(str, index + 1, 
                              k, n, output  + str[index]);
			//swap the array element
			str = this->swap(str, i, index);
		}
	}
	void divisiblePermutation(int num, int k)
	{
		// Converting int num to string
		string text = to_string(num);
		
		// Get the size 
		int n = text.length();
		cout << "\n Given num : " << num;
		cout << "\n Display k : " << k;
		// Find permutation which is divisible by k
		this->permutation(text, 0, k, n, "");
		cout << "\n";
	}
};
int main()
{
	Divisibility *task = new Divisibility();
	// Given number1
	int num = 5032;
	int k = 8;
	task->divisiblePermutation(num, k);
	// Given number2
	num = 45261;
	k = 7;
	task->divisiblePermutation(num, k);
	return 0;
}

Output

 Given num : 5032
 Display k : 8
 5032
 5320
 3520

 Given num : 45261
 Display k : 7
 45612
 42651
 41265
 54621
 52416
 52164
 25641
 26145
 21546
 65142
 64512
 61425
 15246
 15624
 16254
// Include namespace system
using System;
/*
    Csharp Program
    Display all possible permutations of a number divisible by K
*/
public class Divisibility
{
	// Swap two elements in given string
	// i and j is location
	public void swap(char[] str, int i, int j)
	{
		char temp = str[i];
		str[i] = str[j];
		str[j] = temp;
	}
	public void permutation(char[] str, int index, int k, 
                            int n, String output)
	{
		if (index == n)
		{
			// We consider n digit result.
			// So decimal numbers are not start to zero.
			if (output.Length != 0 && output[0] != '0' && index > 1)
			{
				if (Int32.Parse(output) % k == 0)
				{
					Console.Write("\n " + output);
				}
			}
			return;
		}
		for (int i = index; i < n; ++i)
		{
			//swap the array element
			this.swap(str, i, index);
			this.permutation(str, index + 1, k, n, output + str[index]);
			//swap the array element
			this.swap(str, i, index);
		}
	}
	public void divisiblePermutation(int num, int k)
	{
		// Converting int num to string
		String text = "" + num;
		// Convert a string into char array
		char[] str = text.ToCharArray();
		// Get the size 
		int n = text.Length;
		Console.Write("\n Given num : " + num);
		Console.Write("\n Display k : " + k);
		// Find permutation which is divisible by k
		this.permutation(str, 0, k, n, "");
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Given number1
		int num = 5032;
		int k = 8;
		task.divisiblePermutation(num, k);
		// Given number2
		num = 45261;
		k = 7;
		task.divisiblePermutation(num, k);
	}
}

Output

 Given num : 5032
 Display k : 8
 5032
 5320
 3520

 Given num : 45261
 Display k : 7
 45612
 42651
 41265
 54621
 52416
 52164
 25641
 26145
 21546
 65142
 64512
 61425
 15246
 15624
 16254
package main

import "strconv"
import "fmt"
/*
    Go Program
    Display all possible permutations of a 
    number divisible by K
*/

// Swap two elements in given string
// i and j is location
func swap(str[]rune, i int, j int) {
	var temp  = str[i]
	str[i] = str[j]
	str[j] = temp
}
func permutation(str[]rune, index int, k int,
				 n int, output string) {
	if index == n {
		// We consider n digit result.
		// So decimal numbers are not start to zero.
		if len(output) != 0 && output[0] != '0' && index > 1 {
			var num , err = strconv.Atoi(output);
			if err == nil && (num % k) == 0 {
				fmt.Print("\n ", output)
			}
		}
		return
	}
	for i := index ; i < n ; i++ {
		//swap the array element
		swap(str, i, index)
		permutation(str, index + 1, k, n, 
			output + string(str[index]))
		//swap the array element
		swap(str, i, index)
	}
}
func divisiblePermutation(num, k int) {
	// Converting int num to string
	var text string = strconv.Itoa(num)


	// Convert a string into char array
	// Get the size 
	var n int = len(text)

	var str = []rune(text)
	fmt.Print("\n Given num : ", num)
	fmt.Print("\n Display k : ", k)
	// Find permutation which is divisible by k
	permutation(str, 0, k, n, "")
	fmt.Print("\n")
}
func main() {
	
	// Given number1
	var num int = 5032
	var k int = 8
	divisiblePermutation(num, k)
	// Given number2
	num = 45261
	k = 7
	divisiblePermutation(num, k)
}

Output

 Given num : 5032
 Display k : 8
 5032
 5320
 3520

 Given num : 45261
 Display k : 7
 45612
 42651
 41265
 54621
 52416
 52164
 25641
 26145
 21546
 65142
 64512
 61425
 15246
 15624
 16254
<?php
/*
    Php Program
    Display all possible permutations of a number divisible by K
*/
class Divisibility
{
	// Swap two elements in given string
	// i and j is location
	public function swap($str, $i, $j)
	{
		$temp = $str[$i];
		$str[$i] = $str[$j];
		$str[$j] = $temp;
      	return $str;
	}
	public	function permutation($str, $index, $k, $n, $output)
	{
		if ($index == $n)
		{
			// We consider n digit result.
			// So decimal numbers are not start to zero.
			if (strlen($output) != 0 && $output[0] != '0' && $index > 1)
			{
				if (((int)$output) % $k == 0)
				{
					echo("\n ".$output);
				}
			}
			return;
		}
		for ($i = $index; $i < $n; ++$i)
		{
			//swap the array element
			$str = $this->swap($str, $i, $index);
			$this->permutation($str, $index + 1, $k, $n,
                               $output.strval($str[$index]));
			//swap the array element
			$str = $this->swap($str, $i, $index);
		}
	}
	public	function divisiblePermutation($num, $k)
	{
		// Converting int num to string
		$text = "".strval($num);
		// Get the size 
		$n = strlen($text);
		echo("\n Given num : ".$num);
		echo("\n Display k : ".$k);
		// Find permutation which is divisible by k
		$this->permutation($text, 0, $k, $n, "");
		echo("\n");
	}
}

function main()
{
	$task = new Divisibility();
	// Given number1
	$num = 5032;
	$k = 8;
	$task->divisiblePermutation($num, $k);
	// Given number2
	$num = 45261;
	$k = 7;
	$task->divisiblePermutation($num, $k);
}
main();

Output

 Given num : 5032
 Display k : 8
 5032
 5320
 3520

 Given num : 45261
 Display k : 7
 45612
 42651
 41265
 54621
 52416
 52164
 25641
 26145
 21546
 65142
 64512
 61425
 15246
 15624
 16254
/*
    Node JS Program
    Display all possible permutations of a number divisible by K
*/
class Divisibility
{
	// Swap two elements in given string
	// i and j is location
	swap(str, i, j)
	{
		var temp = str[i];
		str[i] = str[j];
		str[j] = temp;
	}
	permutation(str, index, k, n, output)
	{
		if (index == n)
		{
			// We consider n digit result.
			// So decimal numbers are not start to zero.
			if (output.length != 0 && output.charAt(0) != '0' && index > 1)
			{
				if (parseInt(output) % k == 0)
				{
					process.stdout.write("\n " + output);
				}
			}
			return;
		}
		for (var i = index; i < n; ++i)
		{
			//swap the array element
			this.swap(str, i, index);
			this.permutation(str, index + 1, k, n, output + str[index]);
			//swap the array element
			this.swap(str, i, index);
		}
	}
	divisiblePermutation(num, k)
	{
		// Converting int num to string
		var text = "" + num;
		// Convert a string into char array
		var str = (text).split('');
		// Get the size 
		var n = text.length;
		process.stdout.write("\n Given num : " + num);
		process.stdout.write("\n Display k : " + k);
		// Find permutation which is divisible by k
		this.permutation(str, 0, k, n, "");
		process.stdout.write("\n");
	}
}

function main()
{
	var task = new Divisibility();
	// Given number1
	var num = 5032;
	var k = 8;
	task.divisiblePermutation(num, k);
	// Given number2
	num = 45261;
	k = 7;
	task.divisiblePermutation(num, k);
}
main();

Output

 Given num : 5032
 Display k : 8
 5032
 5320
 3520

 Given num : 45261
 Display k : 7
 45612
 42651
 41265
 54621
 52416
 52164
 25641
 26145
 21546
 65142
 64512
 61425
 15246
 15624
 16254
#    Python 3 Program
#    Display all possible permutations of a number divisible by K
class Divisibility :
	#  Swap two elements in given string
	#  i and j is location
	def swap(self, s, i, j) :
		temp = s[i]
		s[i] = s[j]
		s[j] = temp
	
	def permutation(self, s, index, k, n, output) :
		if (index == n) :
			#  We consider n digit result.
			#  So decimal numbers are not start to zero.
			if (len(output) != 0 and output[0] != '0'
				and index > 1) :
				if (int(output) % k == 0) :
					print("\n ", output, end = "")
				
			
			return
		
		i = index
		while (i < n) :
			# swap the list element
			self.swap(s, i, index)
			self.permutation(s, index + 1, k, n, output + str(s[index]))
			# swap the list element
			self.swap(s, i, index)
			i += 1
		
	
	def divisiblePermutation(self, num, k) :
		#  Converting int num to string
		text =  str(num)
		#  Convert a string into char list
		s = list(text)
		#  Get the size 
		n = len(text)
		print("\n Given num : ", num, end = "")
		print("\n Display k : ", k, end = "")
		#  Find permutation which is divisible by k
		self.permutation(s, 0, k, n, "")
		print(end = "\n")
	

def main() :
	task = Divisibility()
	#  Given number1
	num = 5032
	k = 8
	task.divisiblePermutation(num, k)
	#  Given number2
	num = 45261
	k = 7
	task.divisiblePermutation(num, k)

if __name__ == "__main__": main()

Output

 Given num :  5032
 Display k :  8
  5032
  5320
  3520

 Given num :  45261
 Display k :  7
  45612
  42651
  41265
  54621
  52416
  52164
  25641
  26145
  21546
  65142
  64512
  61425
  15246
  15624
  16254
#    Ruby Program
#    Display all possible permutations of a number divisible by K
class Divisibility 
	#  Swap two elements in given string
	#  i and j is location
	def swap(str, i, j) 
		temp = str[i]
		str[i] = str[j]
		str[j] = temp
		return str
	end

	def permutation(str, index, k, n, output) 
		if (index == n) 
			#  We consider n digit result.
			#  So decimal numbers are not start to zero.
			if (output.length != 0 && output[0] != '0' && index > 1) 
				if (Integer(output) % k == 0) 
					print("\n ", output)
				end

			end

			return
		end

		i = index
		while (i < n) 
			# swap the array element
			str = self.swap(str, i, index)
			self.permutation(str, index + 1, k, n, output + str[index].to_s)
			# swap the array element
			str = self.swap(str, i, index)
			i += 1
		end

	end

	def divisiblePermutation(num, k) 
		#  Converting int num to string
		text = ""+ num.to_s
		#  Convert a string into char array
		str = text.chars
		#  Get the size 
		n = text.length
		print("\n Given num : ", num)
		print("\n Display k : ", k)
		#  Find permutation which is divisible by k
		self.permutation(str, 0, k, n, "")
		print("\n")
	end

end

def main() 
	task = Divisibility.new()
	#  Given number1
	num = 5032
	k = 8
	task.divisiblePermutation(num, k)
	#  Given number2
	num = 45261
	k = 7
	task.divisiblePermutation(num, k)
end

main()

Output

 Given num : 5032
 Display k : 8
 5032
 5320
 3520

 Given num : 45261
 Display k : 7
 45612
 42651
 41265
 54621
 52416
 52164
 25641
 26145
 21546
 65142
 64512
 61425
 15246
 15624
 16254
import scala.collection.mutable._;
/*
    Scala Program
    Display all possible permutations of a number divisible by K
*/
class Divisibility()
{
	// Swap two elements in given string
	// i and j is location
	def swap(str: Array[Char], i: Int, j: Int): Unit = {
		var temp: Char = str(i);
		str(i) = str(j);
		str(j) = temp;
	}
	def permutation(str: Array[Char], 
      index: Int, k: Int, n: Int, output: String): Unit = {
		if (index == n)
		{
			// We consider n digit result.
			// So decimal numbers are not start to zero.
			if (output.length() != 0 && 
                output.charAt(0) != '0' && index > 1)
			{
				if (Integer.parseInt(output) % k == 0)
				{
					print("\n " + output);
				}
			}
			return;
		}
		var i: Int = index;
		while (i < n)
		{
			//swap the array element
			swap(str, i, index);
			permutation(str, index + 1, k, 
                        n, output + str(index).toString());
			//swap the array element
			swap(str, i, index);
			i += 1;
		}
	}
	def divisiblePermutation(num: Int, k: Int): Unit = {
		// Converting int num to string
		var text: String = "" + num.toString();
		// Convert a string into char array
		var str: Array[Char] = text.toCharArray();
		// Get the size 
		var n: Int = text.length();
		print("\n Given num : " + num);
		print("\n Display k : " + k);
		// Find permutation which is divisible by k
		permutation(str, 0, k, n, "");
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Divisibility = new Divisibility();
		// Given number1
		var num: Int = 5032;
		var k: Int = 8;
		task.divisiblePermutation(num, k);
		// Given number2
		num = 45261;
		k = 7;
		task.divisiblePermutation(num, k);
	}
}

Output

 Given num : 5032
 Display k : 8
 5032
 5320
 3520

 Given num : 45261
 Display k : 7
 45612
 42651
 41265
 54621
 52416
 52164
 25641
 26145
 21546
 65142
 64512
 61425
 15246
 15624
 16254
import Foundation;
/*
    Swift 4 Program
    Display all possible permutations of a number divisible by K
*/
class Divisibility
{
	// Swap two elements in given string
	// i and j is location
	func swap(_ str: inout[Character], _ i: Int, _ j: Int)
	{
		let temp: Character = str[i];
		str[i] = str[j];
		str[j] = temp;
	}
	func permutation(_ str: inout[Character], 
      _ index: Int, _ k: Int, 
        _ n: Int, _ output: String)
	{
		if (index == n)
		{
			// We consider n digit result.
			// So decimal numbers are not start to zero.
			if (output.count  != 0 && Array(output)[0]  != "0" && index > 1)
			{
				if (Int(output)! % k == 0)
				{
					print("\n ", output, terminator: "");
				}
			}
			return;
		}
		var i: Int = index;
		while (i < n)
		{
			//swap the array element
			self.swap(&str, i, index);
			self.permutation(&str, index + 1, 
                             k, n, output + String(str[index]));
			//swap the array element
			self.swap(&str, i, index);
			i += 1;
		}
	}
	func divisiblePermutation(_ num: Int, _ k: Int)
	{
		// Converting int num to string
		let text: String =  String(num);
		// Convert a string into char array
		var str: [Character] = Array(text);
		// Get the size 
		let n: Int = text.count;
		print("\n Given num : ", num, terminator: "");
		print("\n Display k : ", k, terminator: "");
		// Find permutation which is divisible by k
		self.permutation(&str, 0, k, n, "");
		print(terminator: "\n");
	}
}
func main()
{
	let task: Divisibility = Divisibility();
	// Given number1
	var num: Int = 5032;
	var k: Int = 8;
	task.divisiblePermutation(num, k);
	// Given number2
	num = 45261;
	k = 7;
	task.divisiblePermutation(num, k);
}
main();

Output

 Given num :  5032
 Display k :  8
  5032
  5320
  3520

 Given num :  45261
 Display k :  7
  45612
  42651
  41265
  54621
  52416
  52164
  25641
  26145
  21546
  65142
  64512
  61425
  15246
  15624
  16254
/*
    Kotlin Program
    Display all possible permutations of a number divisible by K
*/
class Divisibility
{
	// Swap two elements in given string
	// i and j is location
	fun swap(str: Array < Char > , i: Int, j: Int): Unit
	{
		val temp: Char = str[i];
		str[i] = str[j];
		str[j] = temp;
	}
	fun permutation(str: Array < Char > , 
                    index: Int, k: Int, n: Int, 
                    output: String): Unit
	{
		if (index == n)
		{
			// We consider n digit result.
			// So decimal numbers are not start to zero.
			if (output.length != 0 && 
                output.get(0) != '0' && index > 1)
			{
				if (output.toInt() % k == 0)
				{
					print("\n " + output);
				}
			}
			return;
		}
		var i: Int = index;
		while (i < n)
		{
			//swap the array element
			this.swap(str, i, index);
			this.permutation(str, index + 1, k, n, 
                             output + str[index].toString());
			//swap the array element
			this.swap(str, i, index);
			i += 1;
		}
	}
	fun divisiblePermutation(num: Int, k: Int): Unit
	{
		// Converting int num to string
		val text: String = "" + num.toString();
		// Convert a string into char array
		val str: Array < Char > = text.toCharArray().toTypedArray();
		// Get the size 
		val n: Int = text.length;
		print("\n Given num : " + num);
		print("\n Display k : " + k);
		// Find permutation which is divisible by k
		this.permutation(str, 0, k, n, "");
		print("\n");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Divisibility = Divisibility();
	// Given number1
	var num: Int = 5032;
	var k: Int = 8;
	task.divisiblePermutation(num, k);
	// Given number2
	num = 45261;
	k = 7;
	task.divisiblePermutation(num, k);
}

Output

 Given num : 5032
 Display k : 8
 5032
 5320
 3520

 Given num : 45261
 Display k : 7
 45612
 42651
 41265
 54621
 52416
 52164
 25641
 26145
 21546
 65142
 64512
 61425
 15246
 15624
 16254




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.

New Comment