Generate all cyclic permutations of a number

Here given code implementation process.

/*
  C Program 
  Generate all cyclic permutations of a number
*/
#include <stdio.h>

// Get number multiplier to permutations
int multiplier(int num)
{
	int ans = 0;
	while (num != 0)
	{
		if (ans == 0)
		{
			ans = 1;
		}
		else
		{
			ans = ans *10;
		}
		num /= 10;
	}
	return ans;
}
// Find all cyclic permutations in number form
void permutation(int num, int actual, int multiply)
{
	printf(" %d\n", num);
	int next = (num % 10) *multiply + (num / 10);
	if (next != actual)
	{
		permutation(next, actual, multiply);
	}
}
// Handles the request of finding cyclic Permutation
void cyclicPermutations(int num)
{
	if (num < 0)
	{
		// When number is less then zero
		return;
	}
	int multiply = multiplier(num);
	printf("\n Cyclic Permutation Of Number %d are \n", num);
	permutation(num, num, multiply);
}
int main()
{
	// Test
	cyclicPermutations(76832);
	cyclicPermutations(123456);
	cyclicPermutations(420);
	return 0;
}

Output

 Cyclic Permutation Of Number 76832 are
 76832
 27683
 32768
 83276
 68327

 Cyclic Permutation Of Number 123456 are
 123456
 612345
 561234
 456123
 345612
 234561

 Cyclic Permutation Of Number 420 are
 420
 42
 204
/*
  Java Program
  Generate all cyclic permutations of a number
*/
class Permutations
{
	// Get number multiplier to permutations
	public int multiplier(int num)
	{
		int ans = 0;
		while (num != 0)
		{
			if (ans == 0)
			{
				ans = 1;
			}
			else
			{
				ans = ans * 10;
			}
			num /= 10;
		}
		return ans;
	}
	// Find all cyclic permutations in number form
	public void permutation(int num, int actual, int multiply)
	{
		System.out.print(" " + num + "\n");
		int next = (num % 10) * multiply + (num / 10);
		if (next != actual)
		{
			permutation(next, actual, multiply);
		}
	}
	// Handles the request of finding cyclic Permutation
	public void cyclicPermutations(int num)
	{
		if (num < 0)
		{
			// When number is less then zero
			return;
		}
		int multiply = multiplier(num);
		System.out.print("\n Cyclic Permutation Of Number " + num + " are \n");
		permutation(num, num, multiply);
	}
	public static void main(String[] args)
	{
		Permutations task = new Permutations();
		// Test
		task.cyclicPermutations(76832);
		task.cyclicPermutations(123456);
		task.cyclicPermutations(420);
	}
}

Output

 Cyclic Permutation Of Number 76832 are
 76832
 27683
 32768
 83276
 68327

 Cyclic Permutation Of Number 123456 are
 123456
 612345
 561234
 456123
 345612
 234561

 Cyclic Permutation Of Number 420 are
 420
 42
 204
// Include header file
#include <iostream>
using namespace std;
/*
  C++ Program
  Generate all cyclic permutations of a number
*/
class Permutations
{
	public:
		// Get number multiplier to permutations
		int multiplier(int num)
		{
			int ans = 0;
			while (num != 0)
			{
				if (ans == 0)
				{
					ans = 1;
				}
				else
				{
					ans = ans *10;
				}
				num /= 10;
			}
			return ans;
		}
	// Find all cyclic permutations in number form
	void permutation(int num, int actual, int multiply)
	{
		cout << " " << num << "\n";
		int next = (num % 10) *multiply + (num / 10);
		if (next != actual)
		{
			this->permutation(next, actual, multiply);
		}
	}
	// Handles the request of finding cyclic Permutation
	void cyclicPermutations(int num)
	{
		// When number is less then zero
		if (num < 0)
		{
			return;
		}
		int multiply = this->multiplier(num);
		cout << "\n Cyclic Permutation Of Number " << num << " are \n";
		this->permutation(num, num, multiply);
	}
};
int main()
{
	Permutations task = Permutations();
	// Test
	task.cyclicPermutations(76832);
	task.cyclicPermutations(123456);
	task.cyclicPermutations(420);
	return 0;
}

Output

 Cyclic Permutation Of Number 76832 are
 76832
 27683
 32768
 83276
 68327

 Cyclic Permutation Of Number 123456 are
 123456
 612345
 561234
 456123
 345612
 234561

 Cyclic Permutation Of Number 420 are
 420
 42
 204
// Include namespace system
using System;
/*
  C# Program
  Generate all cyclic permutations of a number
*/
public class Permutations
{
	// Get number multiplier to permutations
	public int multiplier(int num)
	{
		int ans = 0;
		while (num != 0)
		{
			if (ans == 0)
			{
				ans = 1;
			}
			else
			{
				ans = ans * 10;
			}
			num /= 10;
		}
		return ans;
	}
	// Find all cyclic permutations in number form
	public void permutation(int num, int actual, int multiply)
	{
		Console.Write(" " + num + "\n");
		int next = (num % 10) * multiply + (num / 10);
		if (next != actual)
		{
			permutation(next, actual, multiply);
		}
	}
	// Handles the request of finding cyclic Permutation
	public void cyclicPermutations(int num)
	{
		// When number is less then zero
		if (num < 0)
		{
			return;
		}
		int multiply = multiplier(num);
		Console.Write("\n Cyclic Permutation Of Number " + num + " are \n");
		permutation(num, num, multiply);
	}
	public static void Main(String[] args)
	{
		Permutations task = new Permutations();
		// Test
		task.cyclicPermutations(76832);
		task.cyclicPermutations(123456);
		task.cyclicPermutations(420);
	}
}

Output

 Cyclic Permutation Of Number 76832 are
 76832
 27683
 32768
 83276
 68327

 Cyclic Permutation Of Number 123456 are
 123456
 612345
 561234
 456123
 345612
 234561

 Cyclic Permutation Of Number 420 are
 420
 42
 204
<?php
/*
  Php Program
  Generate all cyclic permutations of a number
*/
class Permutations
{
	// Get number multiplier to permutations
	public	function multiplier($num)
	{
		$ans = 0;
		while ($num != 0)
		{
			if ($ans == 0)
			{
				$ans = 1;
			}
			else
			{
				$ans = $ans * 10;
			}
			$num = intval($num / 10);
		}
		return $ans;
	}
	// Find all cyclic permutations in number form
	public	function permutation($num, $actual, $multiply)
	{
		echo " ". $num ."\n";
		$next = ($num % 10) * $multiply + (intval($num / 10));
		if ($next != $actual)
		{
			$this->permutation($next, $actual, $multiply);
		}
	}
	// Handles the request of finding cyclic Permutation
	public	function cyclicPermutations($num)
	{
		// When number is less then zero
		if ($num < 0)
		{
			return;
		}
		$multiply = $this->multiplier($num);
		echo "\n Cyclic Permutation Of Number ". $num ." are \n";
		$this->permutation($num, $num, $multiply);
	}
}

function main()
{
	$task = new Permutations();
	// Test
	$task->cyclicPermutations(76832);
	$task->cyclicPermutations(123456);
	$task->cyclicPermutations(420);
}
main();

Output

 Cyclic Permutation Of Number 76832 are
 76832
 27683
 32768
 83276
 68327

 Cyclic Permutation Of Number 123456 are
 123456
 612345
 561234
 456123
 345612
 234561

 Cyclic Permutation Of Number 420 are
 420
 42
 204
/*
  Node Js Program
  Generate all cyclic permutations of a number
*/
class Permutations
{
	// Get number multiplier to permutations
	multiplier(num)
	{
		var ans = 0;
		while (num != 0)
		{
			if (ans == 0)
			{
				ans = 1;
			}
			else
			{
				ans = ans * 10;
			}
			num = parseInt(num / 10);
		}
		return ans;
	}
	// Find all cyclic permutations in number form
	permutation(num, actual, multiply)
	{
		process.stdout.write(" " + num + "\n");
		var next = (num % 10) * multiply + (parseInt(num / 10));
		if (next != actual)
		{
			this.permutation(next, actual, multiply);
		}
	}
	// Handles the request of finding cyclic Permutation
	cyclicPermutations(num)
	{
		// When number is less then zero
		if (num < 0)
		{
			return;
		}
		var multiply = this.multiplier(num);
		process.stdout.write("\n Cyclic Permutation Of Number " + num + " are \n");
		this.permutation(num, num, multiply);
	}
}

function main()
{
	var task = new Permutations();
	// Test
	task.cyclicPermutations(76832);
	task.cyclicPermutations(123456);
	task.cyclicPermutations(420);
}
main();

Output

 Cyclic Permutation Of Number 76832 are
 76832
 27683
 32768
 83276
 68327

 Cyclic Permutation Of Number 123456 are
 123456
 612345
 561234
 456123
 345612
 234561

 Cyclic Permutation Of Number 420 are
 420
 42
 204
#   Python 3 Program
#   Generate all cyclic permutations of a number

class Permutations :
	#  Get number multiplier to permutations
	def multiplier(self, num) :
		ans = 0
		while (num != 0) :
			if (ans == 0) :
				ans = 1
			else :
				ans = ans * 10
			
			num = int(num / 10)
		
		return ans
	
	#  Find all cyclic permutations in number form
	def permutation(self, num, actual, multiply) :
		print(" ", num )
		next = (num % 10) * multiply + (int(num / 10))
		if (next != actual) :
			self.permutation(next, actual, multiply)
		
	
	#  Handles the request of finding cyclic Permutation
	def cyclicPermutations(self, num) :
		if (num < 0) :
			#  When number is less then zero
			return
		
		multiply = self.multiplier(num)
		print("\n Cyclic Permutation Of Number ", num ," are ")
		self.permutation(num, num, multiply)
	

def main() :
	task = Permutations()
	#  Test
	task.cyclicPermutations(76832)
	task.cyclicPermutations(123456)
	task.cyclicPermutations(420)

if __name__ == "__main__": main()

Output

 Cyclic Permutation Of Number  76832  are
  76832
  27683
  32768
  83276
  68327

 Cyclic Permutation Of Number  123456  are
  123456
  612345
  561234
  456123
  345612
  234561

 Cyclic Permutation Of Number  420  are
  420
  42
  204
#   Ruby Program
#   Generate all cyclic permutations of a number

class Permutations 
	#  Get number multiplier to permutations
	def multiplier(num) 
		ans = 0
		while (num != 0) 
			if (ans == 0) 
				ans = 1
			else 
				ans = ans * 10
			end

			num /= 10
		end

		return ans
	end

	#  Find all cyclic permutations in number form
	def permutation(num, actual, multiply) 
		print(" ", num ,"\n")
		nextNum = (num % 10) * multiply + (num / 10)
		if (nextNum != actual) 
			self.permutation(nextNum, actual, multiply)
		end

	end

	#  Handles the request of finding cyclic Permutation
	def cyclicPermutations(num) 
		if (num < 0) 
			#  When number is less then zero
			return
		end

		multiply = self.multiplier(num)
		print("\n Cyclic Permutation Of Number ", num ," are \n")
		self.permutation(num, num, multiply)
	end

end

def main() 
	task = Permutations.new()
	#  Test
	task.cyclicPermutations(76832)
	task.cyclicPermutations(123456)
	task.cyclicPermutations(420)
end

main()

Output

 Cyclic Permutation Of Number 76832 are 
 76832
 27683
 32768
 83276
 68327

 Cyclic Permutation Of Number 123456 are 
 123456
 612345
 561234
 456123
 345612
 234561

 Cyclic Permutation Of Number 420 are 
 420
 42
 204
/*
  Scala Program
  Generate all cyclic permutations of a number
*/
class Permutations
{
	// Get number multiplier to permutations
	def multiplier(num: Int): Int = {
      	var n = num;
		var ans: Int = 0;
		while (n != 0)
		{
			if (ans == 0)
			{
				ans = 1;
			}
			else
			{
				ans = ans * 10;
			}
			n = (n / 10).toInt;
		}
		return ans;
	}
	// Find all cyclic permutations in number form
	def permutation(num: Int, actual: Int, multiply: Int): Unit = {
		print(" " + num + "\n");
		var next: Int = (num % 10) * multiply + ((num / 10).toInt);
		if (next != actual)
		{
			this.permutation(next, actual, multiply);
		}
	}
	// Handles the request of finding cyclic Permutation
	def cyclicPermutations(num: Int): Unit = {
		// When number is less then zero
		if (num < 0)
		{
			return;
		}
		var multiply: Int = this.multiplier(num);
		print("\n Cyclic Permutation Of Number " + num + " are \n");
		this.permutation(num, num, multiply);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Permutations = new Permutations();
		// Test
		task.cyclicPermutations(76832);
		task.cyclicPermutations(123456);
		task.cyclicPermutations(420);
	}
}

Output

 Cyclic Permutation Of Number 76832 are
 76832
 27683
 32768
 83276
 68327

 Cyclic Permutation Of Number 123456 are
 123456
 612345
 561234
 456123
 345612
 234561

 Cyclic Permutation Of Number 420 are
 420
 42
 204
/*
  Swift 4 Program
  Generate all cyclic permutations of a number
*/
class Permutations
{
	// Get number multiplier to permutations
	func multiplier(_ num: Int)->Int
	{	var n  = num;
		var ans: Int = 0;
		while (n  != 0)
		{
			if (ans == 0)
			{
				ans = 1;
			}
			else
			{
				ans = ans * 10;
			}
			n /= 10;
		}
		return ans;
	}
	// Find all cyclic permutations in number form
	func permutation(_ num: Int, _ actual: Int, _ multiply: Int)
	{
		print(" ", num );
		let next: Int = (num % 10) * multiply + (num / 10);
		if (next  != actual)
		{
			self.permutation(next, actual, multiply);
		}
	}
	// Handles the request of finding cyclic Permutation
	func cyclicPermutations(_ num: Int)
	{
		// When number is less then zero
		if (num < 0)
		{
			return;
		}
		let multiply: Int = self.multiplier(num);
		print("\n Cyclic Permutation Of Number ", num ," are ");
		self.permutation(num, num, multiply);
	}
}
func main()
{
	let task: Permutations = Permutations();
	// Test
	task.cyclicPermutations(76832);
	task.cyclicPermutations(123456);
	task.cyclicPermutations(420);
}
main();

Output

 Cyclic Permutation Of Number  76832  are
  76832
  27683
  32768
  83276
  68327

 Cyclic Permutation Of Number  123456  are
  123456
  612345
  561234
  456123
  345612
  234561

 Cyclic Permutation Of Number  420  are
  420
  42
  204
/*
  Kotlin Program
  Generate all cyclic permutations of a number
*/
class Permutations
{
	// Get number multiplier to permutations
	fun multiplier(num: Int): Int
	{	
        var n  = num;
		var ans: Int = 0;
		while (n != 0)
		{
			if (ans == 0)
			{
				ans = 1;
			}
			else
			{
				ans = ans * 10;
			}
			n /= 10;
		}
		return ans;
	}
	// Find all cyclic permutations in number form
	fun permutation(num: Int, actual: Int, multiply: Int): Unit
	{
		print(" " + num + "\n");
		var next: Int = (num % 10) * multiply + (num / 10);
		if (next != actual)
		{
			this.permutation(next, actual, multiply);
		}
	}
	// Handles the request of finding cyclic Permutation
	fun cyclicPermutations(num: Int): Unit
	{
		// When number is less then zero
		if (num < 0)
		{
			return;
		}
		var multiply: Int = this.multiplier(num);
		print("\n Cyclic Permutation Of Number " + num + " are \n");
		this.permutation(num, num, multiply);
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Permutations = Permutations();
	// Test
	task.cyclicPermutations(76832);
	task.cyclicPermutations(123456);
	task.cyclicPermutations(420);
}

Output

 Cyclic Permutation Of Number 76832 are
 76832
 27683
 32768
 83276
 68327

 Cyclic Permutation Of Number 123456 are
 123456
 612345
 561234
 456123
 345612
 234561

 Cyclic Permutation Of Number 420 are
 420
 42
 204

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