Skip to main content

Remove repeated digits in a given number

Here given code implementation process.

/*
  C Program for
  Remove repeated digits in a given number
*/
#include <stdio.h>

// Remove recurring digits in a given number
void removeRecurring(int x)
{
	int num = x;
	if (x < 0)
	{
		// When given number is negative
		num = -num;
	}
	// Define some auxiliary variables
	int back = num % 10;
	int current = 0;
	int ans = back;
	int mul = 10;
	num = num / 10;
	while (num > 0)
	{
		current = num % 10;
		if (current != back)
		{
			// Add current digit at beginning of result
			ans = (current *mul) + ans;
			back = current;
			mul *= 10;
		}
		// reduce number
		num /= 10;
	}
	if (x < 0)
	{
		ans = -ans;
	}
	// Display result
	printf("\n Given number : %d", x);
	printf("\n Output       : %d", ans);
}
int main()
{
	// Test
	removeRecurring(1134332);
	removeRecurring(-123300);
	return 0;
}

Output

 Given number : 1134332
 Output       : 13432
 Given number : -123300
 Output       : -1230
/*
  Java Program for
  Remove repeated digits in a given number
*/
class Recurring
{
	// Remove recurring digits in a given number
	public void removeRecurring(int x)
	{
		int num = x;
		if (x < 0)
		{
			// When given number is negative
			num = -num;
		}
		// Define some auxiliary variables
		int back = num % 10;
		int current = 0;
		int ans = back;
		int mul = 10;
		num = num / 10;
		while (num > 0)
		{
			current = num % 10;
			if (current != back)
			{
				// Add current digit at beginning of result
				ans = (current * mul) + ans;
				back = current;
				mul *= 10;
			}
			// reduce number
			num /= 10;
		}
		if (x < 0)
		{
			ans = -ans;
		}
		// Display result
		System.out.print("\n Given number : " + x);
		System.out.print("\n Output : " + ans);
	}
	public static void main(String[] args)
	{
		Recurring task = new Recurring();
		// Test
		task.removeRecurring(1134332);
		task.removeRecurring(-123300);
	}
}

Output

 Given number : 1134332
 Output : 13432
 Given number : -123300
 Output : -1230
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Remove repeated digits in a given number
*/
class Recurring
{
	public:
		// Remove recurring digits in a given number
		void removeRecurring(int x)
		{
			int num = x;
			if (x < 0)
			{
				// When given number is negative
				num = -num;
			}
			// Define some auxiliary variables
			int back = num % 10;
			int current = 0;
			int ans = back;
			int mul = 10;
			num = num / 10;
			while (num > 0)
			{
				current = num % 10;
				if (current != back)
				{
					// Add current digit at beginning of result
					ans = (current *mul) + ans;
					back = current;
					mul *= 10;
				}
				// reduce number
				num /= 10;
			}
			if (x < 0)
			{
				ans = -ans;
			}
			// Display result
			cout << "\n Given number : " << x;
			cout << "\n Output : " << ans;
		}
};
int main()
{
	Recurring task = Recurring();
	// Test
	task.removeRecurring(1134332);
	task.removeRecurring(-123300);
	return 0;
}

Output

 Given number : 1134332
 Output : 13432
 Given number : -123300
 Output : -1230
// Include namespace system
using System;
/*
  C# Program for
  Remove repeated digits in a given number
*/
public class Recurring
{
	// Remove recurring digits in a given number
	public void removeRecurring(int x)
	{
		int num = x;
		if (x < 0)
		{
			// When given number is negative
			num = -num;
		}
		// Define some auxiliary variables
		int back = num % 10;
		int current = 0;
		int ans = back;
		int mul = 10;
		num = num / 10;
		while (num > 0)
		{
			current = num % 10;
			if (current != back)
			{
				// Add current digit at beginning of result
				ans = (current * mul) + ans;
				back = current;
				mul *= 10;
			}
			// reduce number
			num /= 10;
		}
		if (x < 0)
		{
			ans = -ans;
		}
		// Display result
		Console.Write("\n Given number : " + x);
		Console.Write("\n Output : " + ans);
	}
	public static void Main(String[] args)
	{
		Recurring task = new Recurring();
		// Test
		task.removeRecurring(1134332);
		task.removeRecurring(-123300);
	}
}

Output

 Given number : 1134332
 Output : 13432
 Given number : -123300
 Output : -1230
<?php
/*
  Php Program for
  Remove repeated digits in a given number
*/
class Recurring
{
	// Remove recurring digits in a given number
	public	function removeRecurring($x)
	{
		$num = $x;
		if ($x < 0)
		{
			// When given number is negative
			$num = -$num;
		}
		// Define some auxiliary variables
		$back = $num % 10;
		$current = 0;
		$ans = $back;
		$mul = 10;
		$num = intval($num / 10);
		while ($num > 0)
		{
			$current = $num % 10;
			if ($current != $back)
			{
				// Add current digit at beginning of result
				$ans = ($current * $mul) + $ans;
				$back = $current;
				$mul *= 10;
			}
			// reduce number
			$num = intval($num / 10);
		}
		if ($x < 0)
		{
			$ans = -$ans;
		}
		// Display result
		echo "\n Given number : ". $x;
		echo "\n Output : ". $ans;
	}
}

function main()
{
	$task = new Recurring();
	// Test
	$task->removeRecurring(1134332);
	$task->removeRecurring(-123300);
}
main();

Output

 Given number : 1134332
 Output : 13432
 Given number : -123300
 Output : -1230
/*
  Node Js Program for
  Remove repeated digits in a given number
*/
class Recurring
{
	// Remove recurring digits in a given number
	removeRecurring(x)
	{
		var num = x;
		if (x < 0)
		{
			// When given number is negative
			num = -num;
		}
		// Define some auxiliary variables
		var back = num % 10;
		var current = 0;
		var ans = back;
		var mul = 10;
		num = parseInt(num / 10);
		while (num > 0)
		{
			current = num % 10;
			if (current != back)
			{
				// Add current digit at beginning of result
				ans = (current * mul) + ans;
				back = current;
				mul *= 10;
			}
			// reduce number
			num = parseInt(num / 10);
		}
		if (x < 0)
		{
			ans = -ans;
		}
		// Display result
		process.stdout.write("\n Given number : " + x);
		process.stdout.write("\n Output : " + ans);
	}
}

function main()
{
	var task = new Recurring();
	// Test
	task.removeRecurring(1134332);
	task.removeRecurring(-123300);
}
main();

Output

 Given number : 1134332
 Output : 13432
 Given number : -123300
 Output : -1230
#   Python 3 Program for
#   Remove repeated digits in a given number

class Recurring :
	#  Remove recurring digits in a given number
	def removeRecurring(self, x) :
		num = x
		if (x < 0) :
			#  When given number is negative
			num = -num
		
		#  Define some auxiliary variables
		back = num % 10
		current = 0
		ans = back
		mul = 10
		num = int(num / 10)
		while (num > 0) :
			current = num % 10
			if (current != back) :
				#  Add current digit at beginning of result
				ans = (current * mul) + ans
				back = current
				mul *= 10
			
			num = int(num /
				#  reduce number
				10)
		
		if (x < 0) :
			ans = -ans
		
		#  Display result
		print("\n Given number : ", x, end = "")
		print("\n Output : ", ans, end = "")
	

def main() :
	task = Recurring()
	#  Test
	task.removeRecurring(1134332)
	task.removeRecurring(-123300)

if __name__ == "__main__": main()

Output

 Given number :  1134332
 Output :  13432
 Given number :  -123300
 Output :  -1230
#   Ruby Program for
#   Remove repeated digits in a given number

class Recurring 
	#  Remove recurring digits in a given number
	def removeRecurring(x) 
		num = x
		if (x < 0) 
			#  When given number is negative
			num = -num
		end

		#  Define some auxiliary variables
		back = num % 10
		current = 0
		ans = back
		mul = 10
		num = num / 10
		while (num > 0) 
			current = num % 10
			if (current != back) 
				#  Add current digit at beginning of result
				ans = (current * mul) + ans
				back = current
				mul *= 10
			end

			#  reduce number
			num /= 10
		end

		if (x < 0) 
			ans = -ans
		end

		#  Display result
		print("\n Given number : ", x)
		print("\n Output : ", ans)
	end

end

def main() 
	task = Recurring.new()
	#  Test
	task.removeRecurring(1134332)
	task.removeRecurring(-123300)
end

main()

Output

 Given number : 1134332
 Output : 13432
 Given number : -123300
 Output : -1230
/*
  Scala Program for
  Remove repeated digits in a given number
*/
class Recurring
{
	// Remove recurring digits in a given number
	def removeRecurring(x: Int): Unit = {
		var num: Int = x;
		if (x < 0)
		{
			// When given number is negative
			num = -num;
		}
		// Define some auxiliary variables
		var back: Int = num % 10;
		var current: Int = 0;
		var ans: Int = back;
		var mul: Int = 10;
		num = (num / 10).toInt;
		while (num > 0)
		{
			current = num % 10;
			if (current != back)
			{
				// Add current digit at beginning of result
				ans = (current * mul) + ans;
				back = current;
				mul *= 10;
			}
			// reduce number
			num = (num / 10).toInt;
		}
		if (x < 0)
		{
			ans = -ans;
		}
		// Display result
		print("\n Given number : " + x);
		print("\n Output : " + ans);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Recurring = new Recurring();
		// Test
		task.removeRecurring(1134332);
		task.removeRecurring(-123300);
	}
}

Output

 Given number : 1134332
 Output : 13432
 Given number : -123300
 Output : -1230
/*
  Swift 4 Program for
  Remove repeated digits in a given number
*/
class Recurring
{
	// Remove recurring digits in a given number
	func removeRecurring(_ x: Int)
	{
		var num: Int = x;
		if (x < 0)
		{
			// When given number is negative
			num = -num;
		}
		// Define some auxiliary variables
		var back: Int = num % 10;
		var current: Int = 0;
		var ans: Int = back;
		var mul: Int = 10;
		num = num / 10;
		while (num > 0)
		{
			current = num % 10;
			if (current  != back)
			{
				// Add current digit at beginning of result
				ans = (current * mul) + ans;
				back = current;
				mul *= 10;
			}
			// reduce number
			num /= 10;
		}
		if (x < 0)
		{
			ans = -ans;
		}
		// Display result
		print("\n Given number : ", x, terminator: "");
		print("\n Output : ", ans, terminator: "");
	}
}
func main()
{
	let task: Recurring = Recurring();
	// Test
	task.removeRecurring(1134332);
	task.removeRecurring(-123300);
}
main();

Output

 Given number :  1134332
 Output :  13432
 Given number :  -123300
 Output :  -1230
/*
  Kotlin Program for
  Remove repeated digits in a given number
*/
class Recurring
{
	// Remove recurring digits in a given number
	fun removeRecurring(x: Int): Unit
	{
		var num: Int = x;
		if (x < 0)
		{
			// When given number is negative
			num = -num;
		}
		// Define some auxiliary variables
		var back: Int = num % 10;
		var current: Int ;
		var ans: Int = back;
		var mul: Int = 10;
		num = num / 10;
		while (num > 0)
		{
			current = num % 10;
			if (current != back)
			{
				// Add current digit at beginning of result
				ans = (current * mul) + ans;
				back = current;
				mul *= 10;
			}
			// reduce number
			num /= 10;
		}
		if (x < 0)
		{
			ans = -ans;
		}
		// Display result
		print("\n Given number : " + x);
		print("\n Output : " + ans);
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Recurring = Recurring();
	// Test
	task.removeRecurring(1134332);
	task.removeRecurring(-123300);
}

Output

 Given number : 1134332
 Output : 13432
 Given number : -123300
 Output : -1230




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