Reverse a number using recursion

Here given code implementation process.

//C Program 
//Reverse a number using recursion 
#include <stdio.h>

int reverse_num(int number, int result)
{
	//Base condition to stop the recursion process
	if (number > 0)
	{
		//Recursive function call
		return reverse_num(number / 10, result * 10 + (number % 10));
	}
	//When no digit remaining
	return result;
}
//This is handling the request process of reverse number digit
int reverse(int number)
{
	//Display given number
	printf("\nBefore Number : [%d]\n", number);
	if (number < 0)
	{
		return -reverse_num(-number, 0);
	}
	else
	{
		return reverse_num(number, 0);
	}
}
int main()
{
	//Test Case 
	int number = 12345;
	number = reverse(number);
	//After reverse 
	printf("After Number  : [%d]\n", number);
	number = reverse(78942);
	//After reverse 
	printf("After Number  : [%d]\n", number);
	number = reverse(1020);
	//After reverse 
	printf("After Number  : [%d]\n", number);
	number = reverse(-28);
	//After reverse 
	printf("After Number  : [%d]\n", number);
	return 0;
}

Output

Before Number : [12345]
After Number  : [54321]

Before Number : [78942]
After Number  : [24987]

Before Number : [1020]
After Number  : [201]

Before Number : [-28]
After Number  : [-82]
// Program for
// Reverse a number using recursion in java
class MyString
{
	//This is reversing a number elements recursively
	public int reverse_num(int number, int result)
	{
		//Base condition to stop the recursion process
		if (number > 0)
		{
			//Recursive function call
			return reverse_num(number / 10, result * 10 + (number % 10));
		}
		//When no digit remaining
		return result;
	}
	//This is handling the request process of reverse number digit
	public int reverse(int number)
	{
		//Display given number
		System.out.print("\nBefore Number : [" + number + "]\n");
		if (number < 0)
		{
			return -reverse_num(-number, 0);
		}
		else
		{
			return reverse_num(number, 0);
		}
	}
	public static void main(String[] args)
	{
		MyString obj = new MyString();
		//Test Case 
		int number = 12345;
		number = obj.reverse(number);
		//After reverse	
		System.out.print("After Number  : [" + number + "]\n");
		number = obj.reverse(78942);
		//After reverse	
		System.out.print("After Number  : [" + number + "]\n");
		number = obj.reverse(1020);
		//After reverse	
		System.out.print("After Number  : [" + number + "]\n");
		number = obj.reverse(-28);
		//After reverse	
		System.out.print("After Number  : [" + number + "]\n");
	}
}

Output

Before Number : [12345]
After Number  : [54321]

Before Number : [78942]
After Number  : [24987]

Before Number : [1020]
After Number  : [201]

Before Number : [-28]
After Number  : [-82]
//Include header file
#include <iostream>

using namespace std;
// Program for
// Reverse a number using recursion in C++
class MyString
{
	public:
		//This is reversing a number elements recursively
		int reverse_num(int number, int result)
		{
			//Base condition to stop the recursion process
			if (number > 0)
			{
				//Recursive function call
				return this->reverse_num(number / 10, result * 10 + (number % 10));
			}
			//When no digit remaining
			return result;
		}
	//This is handling the request process of reverse number digit
	int reverse(int number)
	{
		//Display given number
		cout << "\nBefore Number : [" << number << "]\n";
		if (number < 0)
		{
			return -this->reverse_num(-number, 0);
		}
		else
		{
			return this->reverse_num(number, 0);
		}
	}
};
int main()
{
	MyString obj = MyString();
	//Test Case 
	int number = 12345;
	number = obj.reverse(number);
	//After reverse	
	cout << "After Number  : [" << number << "]\n";
	number = obj.reverse(78942);
	//After reverse	
	cout << "After Number  : [" << number << "]\n";
	number = obj.reverse(1020);
	//After reverse	
	cout << "After Number  : [" << number << "]\n";
	number = obj.reverse(-28);
	//After reverse	
	cout << "After Number  : [" << number << "]\n";
	return 0;
}

Output

Before Number : [12345]
After Number  : [54321]

Before Number : [78942]
After Number  : [24987]

Before Number : [1020]
After Number  : [201]

Before Number : [-28]
After Number  : [-82]
//Include namespace system
using System;
// Program for
// Reverse a number using recursion in C#
class MyString
{
	//This is reversing a number elements recursively
	public int reverse_num(int number, int result)
	{
		//Base condition to stop the recursion process
		if (number > 0)
		{
			//Recursive function call
			return reverse_num(number / 10, result * 10 + (number % 10));
		}
		//When no digit remaining
		return result;
	}
	//This is handling the request process of reverse number digit
	public int reverse(int number)
	{
		//Display given number
		Console.Write("\nBefore Number : [" + number + "]\n");
		if (number < 0)
		{
			return -reverse_num(-number, 0);
		}
		else
		{
			return reverse_num(number, 0);
		}
	}
	public static void Main(String[] args)
	{
		MyString obj = new MyString();
		//Test Case 
		int number = 12345;
		number = obj.reverse(number);
		//After reverse	
		Console.Write("After Number  : [" + number + "]\n");
		number = obj.reverse(78942);
		//After reverse	
		Console.Write("After Number  : [" + number + "]\n");
		number = obj.reverse(1020);
		//After reverse	
		Console.Write("After Number  : [" + number + "]\n");
		number = obj.reverse(-28);
		//After reverse	
		Console.Write("After Number  : [" + number + "]\n");
	}
}

Output

Before Number : [12345]
After Number  : [54321]

Before Number : [78942]
After Number  : [24987]

Before Number : [1020]
After Number  : [201]

Before Number : [-28]
After Number  : [-82]
<?php
// Program for
// Reverse a number using recursion in Php
class MyString
{
	//This is reversing a number elements recursively
	public	function reverse_num($number, $result)
	{
		//Base condition to stop the recursion process
		if ($number > 0)
		{
			//Recursive function call
			return $this->reverse_num(intval($number / 10), $result * 10 + ($number % 10));
		}
		//When no digit remaining
		return $result;
	}
	//This is handling the request process of reverse number digit
	public	function reverse($number)
	{
		echo "\nBefore Number : [". $number ."]\n";
		if ($number < 0)
		{
			return -$this->reverse_num(-$number, 0);
		}
		else
		{
			return $this->reverse_num($number, 0);
		}
	}
}

function main()
{
	$obj = new MyString();
	//Test Case 
	$number = 12345;
	$number = $obj->reverse($number);
	echo "After Number  : [". $number ."]\n";
	$number = $obj->reverse(78942);
	echo "After Number  : [". $number ."]\n";
	$number = $obj->reverse(1020);
	echo "After Number  : [". $number ."]\n";
	$number = $obj->reverse(-28);
	echo "After Number  : [". $number ."]\n";
}
main();

Output

Before Number : [12345]
After Number  : [54321]

Before Number : [78942]
After Number  : [24987]

Before Number : [1020]
After Number  : [201]

Before Number : [-28]
After Number  : [-82]
// Program for
// Reverse a number using recursion in Node Js
class MyString
{
	//This is reversing a number elements recursively
	reverse_num(number, result)
	{
		//Base condition to stop the recursion process
		if (number > 0)
		{
			//Recursive function call
			return this.reverse_num(parseInt(number / 10), result * 10 + (number % 10));
		}
		//When no digit remaining
		return result;
	}
	//This is handling the request process of reverse number digit
	reverse(number)
	{
		process.stdout.write("\nBefore Number : [" + number + "]\n");
		if (number < 0)
		{
			return -this.reverse_num(-number, 0);
		}
		else
		{
			return this.reverse_num(number, 0);
		}
	}
}

function main()
{
	var obj = new MyString();
	//Test Case 
	var number = 12345;
	number = obj.reverse(number);
	process.stdout.write("After Number  : [" + number + "]\n");
	number = obj.reverse(78942);
	process.stdout.write("After Number  : [" + number + "]\n");
	number = obj.reverse(1020);
	process.stdout.write("After Number  : [" + number + "]\n");
	number = obj.reverse(-28);
	process.stdout.write("After Number  : [" + number + "]\n");
}
main();

Output

Before Number : [12345]
After Number  : [54321]

Before Number : [78942]
After Number  : [24987]

Before Number : [1020]
After Number  : [201]

Before Number : [-28]
After Number  : [-82]
#  Program for
#  Reverse a number using recursion in Python 3
class MyString :
	# This is reversing a number elements recursively
	def reverse_num(self, number, result) :
		# Base condition to stop the recursion process
		if (number > 0) :
			# Recursive function call
			return self.reverse_num(int(number / 10), result * 10 + (number % 10))
		
		# When no digit remaining
		return result
	
	# This is handling the request process of reverse number digit
	def reverse(self, number) :
		print("\nBefore Number : [", number ,"]\n", end = "")
		if (number < 0) :
			return -self.reverse_num(-number, 0)
		else :
			return self.reverse_num(number, 0)
		
	

def main() :
	obj = MyString()
	# Test Case 
	number = 12345
	number = obj.reverse(number)
	print("After Number  : [", number ,"]\n", end = "")
	number = obj.reverse(78942)
	print("After Number  : [", number ,"]\n", end = "")
	number = obj.reverse(1020)
	print("After Number  : [", number ,"]\n", end = "")
	number = obj.reverse(-28)
	print("After Number  : [", number ,"]\n", end = "")

if __name__ == "__main__": main()

Output

Before Number : [ 12345 ]
After Number  : [ 54321 ]

Before Number : [ 78942 ]
After Number  : [ 24987 ]

Before Number : [ 1020 ]
After Number  : [ 201 ]

Before Number : [ -28 ]
After Number  : [ -82 ]
#  Program for
#  Reverse a number using recursion in Ruby
class MyString

	# This is reversing a number elements recursively
	def reverse_num(number, result)
	
		# Base condition to stop the recursion process
		if (number > 0)
		
			# Recursive function call
			return self.reverse_num(number / 10, result * 10 + (number % 10))
		end
		# When no digit remaining
		return result
	end
	# This is handling the request process of reverse number digit
	def reverse(number)
	
		# Display given number
		print("\nBefore Number : [", number ,"]\n")
		if (number < 0)
		
			return -self.reverse_num(-number, 0)
		else
		
			return self.reverse_num(number, 0)
		end
	end
end
def main()

	obj = MyString.new()
	# Test Case 
	number = 12345
	number = obj.reverse(number)
	# After reverse	
	print("After Number  : [", number ,"]\n")
	number = obj.reverse(78942)
	# After reverse	
	print("After Number  : [", number ,"]\n")
	number = obj.reverse(1020)
	# After reverse	
	print("After Number  : [", number ,"]\n")
	number = obj.reverse(-28)
	# After reverse	
	print("After Number  : [", number ,"]\n")
end
main()

Output

Before Number : [12345]
After Number  : [54321]

Before Number : [78942]
After Number  : [24987]

Before Number : [1020]
After Number  : [201]

Before Number : [-28]
After Number  : [-82]
// Program for
// Reverse a number using recursion in Scala
class MyString
{
	//This is reversing a number elements recursively
	def reverse_num(number: Int, result: Int): Int = {
		//Base condition to stop the recursion process
		if (number > 0)
		{
			//Recursive function call
			return reverse_num((number / 10).toInt, result * 10 + (number % 10));
		}
		//When no digit remaining
		return result;
	}
	//This is handling the request process of reverse number digit
	def reverse(number: Int): Int = {
		//Display given number
		print("\nBefore Number : [" + number + "]\n");
		if (number < 0)
		{
			return -reverse_num(-number, 0);
		}
		else
		{
			return reverse_num(number, 0);
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		//Test Case 
		var number: Int = 12345;
		number = obj.reverse(number);
		//After reverse	
		print("After Number  : [" + number + "]\n");
		number = obj.reverse(78942);
		//After reverse	
		print("After Number  : [" + number + "]\n");
		number = obj.reverse(1020);
		//After reverse	
		print("After Number  : [" + number + "]\n");
		number = obj.reverse(-28);
		//After reverse	
		print("After Number  : [" + number + "]\n");
	}
}

Output

Before Number : [12345]
After Number  : [54321]

Before Number : [78942]
After Number  : [24987]

Before Number : [1020]
After Number  : [201]

Before Number : [-28]
After Number  : [-82]
// Program for
// Reverse a number using recursion in Swift
class MyString
{
	//This is reversing a number elements recursively
	func reverse_num(_ number: Int, _ result: Int) -> Int
	{
		//Base condition to stop the recursion process
		if (number > 0)
		{
			//Recursive function call
			return self.reverse_num(number / 10, result * 10 + (number % 10));
		}
		//When no digit remaining
		return result;
	}
	//This is handling the request process of reverse number digit
	func reverse(_ number: Int) -> Int
	{
		print("\nBefore Number : [", number ,"]\n", terminator: "");
		if (number < 0)
		{
			return -self.reverse_num(-number, 0);
		}
		else
		{
			return self.reverse_num(number, 0);
		}
	}
}
func main()
{
	let obj: MyString = MyString();
	//Test Case 
	var number: Int = 12345;
	number = obj.reverse(number);
	print("After Number  : [", number ,"]\n", terminator: "");
	number = obj.reverse(78942);
	print("After Number  : [", number ,"]\n", terminator: "");
	number = obj.reverse(1020);
	print("After Number  : [", number ,"]\n", terminator: "");
	number = obj.reverse(-28);
	print("After Number  : [", number ,"]\n", terminator: "");
}
main();

Output

Before Number : [ 12345 ]
After Number  : [ 54321 ]

Before Number : [ 78942 ]
After Number  : [ 24987 ]

Before Number : [ 1020 ]
After Number  : [ 201 ]

Before Number : [ -28 ]
After Number  : [ -82 ]

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