Reverse digits of a number

Here given code implementation process.

// C program
// Reverse digits of a number
#include <stdio.h>

void reverseNum(int number)
{
	int result = 0;
	int num = number;
	while (num != 0)
	{
		result = (result *10) + (num % 10);
		num = num / 10;
	}
	printf("\n Number : %d", number);
	// Display calculated result
	printf("\n Reverse  : %d", result);
}
int main()
{
	reverseNum(-53);
	reverseNum(1253);
	return 0;
}

input

 Number : -53
 Reverse  : -35
 Number : 1253
 Reverse  : 3521
/*
  Java Program for
  Reverse digits of a number
*/
class Reverse
{
	// Reverse digits of a given number
	public void reverseNum(int number)
	{
		int result = 0;
		int num = number;
		while (num != 0)
		{
			// Add last digit to end of result
			result = (result * 10) + (num % 10);
			// Remove last digit
			num = num / 10;
		}
		System.out.println(" Number : " + number);
		// Display calculated result
		System.out.println(" Reverse : " + result);
	}
	public static void main(String[] args)
	{
		Reverse task = new Reverse();
		// Test Case
		task.reverseNum(-53);
		task.reverseNum(1253);
	}
}

input

 Number : -53
 Reverse : -35
 Number : 1253
 Reverse : 3521
// Include header file
#include <iostream>
using namespace std;
/*
  C++ Program for
  Reverse digits of a number
*/
class Reverse
{
	public:
		// Reverse digits of a given number
		void reverseNum(int number)
		{
			int result = 0;
			int num = number;
			while (num != 0)
			{
				// Add last digit to end of result
				result = (result *10) + (num % 10);
				// Remove last digit
				num = num / 10;
			}
			cout << " Number : " << number << endl;
			// Display calculated result
			cout << " Reverse : " << result << endl;
		}
};
int main()
{
	Reverse *task = new Reverse();
	// Test Case
	task->reverseNum(-53);
	task->reverseNum(1253);
	return 0;
}

input

 Number : -53
 Reverse : -35
 Number : 1253
 Reverse : 3521
// Include namespace system
using System;
/*
  Csharp Program for
  Reverse digits of a number
*/
public class Reverse
{
	// Reverse digits of a given number
	public void reverseNum(int number)
	{
		int result = 0;
		int num = number;
		while (num != 0)
		{
			// Add last digit to end of result
			result = (result * 10) + (num % 10);
			// Remove last digit
			num = num / 10;
		}
		Console.WriteLine(" Number : " + number);
		// Display calculated result
		Console.WriteLine(" Reverse : " + result);
	}
	public static void Main(String[] args)
	{
		Reverse task = new Reverse();
		// Test Case
		task.reverseNum(-53);
		task.reverseNum(1253);
	}
}

input

 Number : -53
 Reverse : -35
 Number : 1253
 Reverse : 3521
<?php
/*
  Php Program for
  Reverse digits of a number
*/
class Reverse
{
	// Reverse digits of a given number
	public	function reverseNum($number)
	{
		$result = 0;
		$num = $number;
		while ($num != 0)
		{
			// Add last digit to end of result
			$result = ($result * 10) + ($num % 10);
			// Remove last digit
			$num = (int)($num / 10);
		}
		echo " Number : ".$number.
		"\n";
		// Display calculated result
		echo " Reverse : ".$result.
		"\n";
	}
}

function main()
{
	$task = new Reverse();
	// Test Case
	$task->reverseNum(-53);
	$task->reverseNum(1253);
}
main();

input

 Number : -53
 Reverse : -35
 Number : 1253
 Reverse : 3521
/*
  Node JS Program for
  Reverse digits of a number
*/
class Reverse
{
	// Reverse digits of a given number
	reverseNum(number)
	{
		var result = 0;
		var num = number;
		while (num != 0)
		{
			// Add last digit to end of result
			result = (result * 10) + (num % 10);
			// Remove last digit
			num = parseInt(num / 10);
		}
		console.log(" Number : " + number);
		// Display calculated result
		console.log(" Reverse : " + result);
	}
}

function main()
{
	var task = new Reverse();
	// Test Case
	task.reverseNum(-53);
	task.reverseNum(1253);
}
main();

input

 Number : -53
 Reverse : -35
 Number : 1253
 Reverse : 3521
#  Python 3 Program for
#  Reverse digits of a number
class Reverse :
	#  Reverse digits of a given number
	def reverseNum(self, number) :
		result = 0
		num = number
		negative = False
		if (num < 0) :
			negative = True
			num = -num
		
		while (num != 0) :
			#  Add last digit to end of result
			result = (result * 10) + (num % 10)
			#  Remove last digit
			num = int(num / 10)
		
		if (negative) :
			result = -result
		
		print(" Number : ", number)
		#  Display calculated result
		print(" Reverse : ", result)
	

def main() :
	task = Reverse()
	#  Test Case
	task.reverseNum(-53)
	task.reverseNum(1253)

if __name__ == "__main__": main()

input

 Number :  -53
 Reverse :  -35
 Number :  1253
 Reverse :  3521
#  Ruby Program for
#  Reverse digits of a number
class Reverse 
	#  Reverse digits of a given number
	def reverseNum(number) 
		result = 0
		num = number
		negative = false
		if (num < 0) 
			negative = true
			num = -num
		end

		while (num != 0) 
			#  Add last digit to end of result
			result = (result * 10) + (num % 10)
			#  Remove last digit
			num = num / 10
		end

		if (negative) 
			result = -result
		end

		print(" Number : ", number, "\n")
		#  Display calculated result
		print(" Reverse : ", result, "\n")
	end

end

def main() 
	task = Reverse.new()
	#  Test Case
	task.reverseNum(-53)
	task.reverseNum(1253)
end

main()

input

 Number : -53
 Reverse : -35
 Number : 1253
 Reverse : 3521
/*
  Scala Program for
  Reverse digits of a number
*/
class Reverse()
{
	// Reverse digits of a given number
	def reverseNum(number: Int): Unit = {
		var result: Int = 0;
		var num: Int = number;
		var negative: Boolean = false;
		if (num < 0)
		{
			negative = true;
			num = -num;
		}
		while (num != 0)
		{
			// Add last digit to end of result
			result = (result * 10) + (num % 10);
			// Remove last digit
			num = (num / 10).toInt;
		}
		if (negative)
		{
			result = -result;
		}
		println(" Number : " + number);
		// Display calculated result
		println(" Reverse : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Reverse = new Reverse();
		// Test Case
		task.reverseNum(-53);
		task.reverseNum(1253);
	}
}

input

 Number : -53
 Reverse : -35
 Number : 1253
 Reverse : 3521
/*
  Swift 4 Program for
  Reverse digits of a number
*/
class Reverse
{
	// Reverse digits of a given number
	func reverseNum(_ number: Int)
	{
		var result: Int = 0;
		var num: Int = number;
		var negative: Bool = false;
		if (num < 0)
		{
			negative = true;
			num = -num;
		}
		while (num  != 0)
		{
			// Add last digit to end of result
			result = (result * 10) + (num % 10);
			// Remove last digit
			num = num / 10;
		}
		if (negative)
		{
			result = -result;
		}
		print(" Number : ", number);
		// Display calculated result
		print(" Reverse : ", result);
	}
}
func main()
{
	let task: Reverse = Reverse();
	// Test Case
	task.reverseNum(-53);
	task.reverseNum(1253);
}
main();

input

 Number :  -53
 Reverse :  -35
 Number :  1253
 Reverse :  3521
/*
  Kotlin Program for
  Reverse digits of a number
*/
class Reverse
{
	// Reverse digits of a given number
	fun reverseNum(number: Int): Unit
	{
		var result: Int = 0;
		var num: Int = number;
		var negative: Boolean = false;
		if (num < 0)
		{
			negative = true;
			num = -num;
		}
		while (num != 0)
		{
			// Add last digit to end of result
			result = (result * 10) + (num % 10);
			// Remove last digit
			num = num / 10;
		}
		if (negative)
		{
			result = -result;
		}
		println(" Number : " + number);
		// Display calculated result
		println(" Reverse : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Reverse = Reverse();
	// Test Case
	task.reverseNum(-53);
	task.reverseNum(1253);
}

input

 Number : -53
 Reverse : -35
 Number : 1253
 Reverse : 3521


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