# Write a program to reverse digits of a number

Write a program to reverse the digits of an integer.

Examples :

```Input : num = 12345
Output: 54321

Input : num = 876
Output: 678```

Flowchart:

ITERATIVE WAY

Algorithm:

```Input:  num
(1) Initialize rev_num = 0
(2) Loop while num > 0
(a) Multiply rev_num by 10 and add remainder of num
divide by 10 to rev_num
rev_num = rev_num*10 + num%10;
(b) Divide num by 10
(3) Return rev_num```

Example:

num = 4562
rev_num = 0
rev_num = rev_num *10 + num%10 = 2
num = num/10 = 456
rev_num = rev_num *10 + num%10 = 20 + 6 = 26
num = num/10 = 45
rev_num = rev_num *10 + num%10 = 260 + 5 = 265
num = num/10 = 4
rev_num = rev_num *10 + num%10 = 2650 + 4 = 2654
num = num/10 = 0

Program:

## C

 `#include `` ` `/* Iterative function to reverse digits of num*/``int` `reverseDigits(``int` `num) ``{ ``    ``int` `rev_num = 0; ``    ``while` `(num > 0) { ``        ``rev_num = rev_num * 10 + num % 10; ``        ``num = num / 10; ``    ``} ``    ``return` `rev_num; ``} `` ` `/*Driver program to test reverseDigits*/``int` `main() ``{ ``    ``int` `num = 4562; ``    ``printf``(``"Reverse of no. is %d"``, reverseDigits(num)); `` ` `    ``getchar``(); ``    ``return` `0; ``}`

## C++

 `#include `` ` `using` `namespace` `std; ``/* Iterative function to reverse digits of num*/``int` `reverseDigits(``int` `num) ``{ ``    ``int` `rev_num = 0; ``    ``while` `(num > 0) { ``        ``rev_num = rev_num * 10 + num % 10; ``        ``num = num / 10; ``    ``} ``    ``return` `rev_num; ``} `` ` `/*Driver program to test reverseDigits*/``int` `main() ``{ ``    ``int` `num = 4562; ``    ``cout << ``"Reverse of no. is "` `<< reverseDigits(num); ``    ``getchar``(); ``    ``return` `0; ``} `` ` `// This code is contributed ``// by Akanksha Rai(Abby_akku)`

## Java

 `// Java program to reverse a number `` ` `class` `GFG { ``    ``/* Iterative function to reverse ``    ``digits of num*/``    ``static` `int` `reverseDigits(``int` `num) ``    ``{ ``        ``int` `rev_num = ``0``; ``        ``while` `(num > ``0``) { ``            ``rev_num = rev_num * ``10` `+ num % ``10``; ``            ``num = num / ``10``; ``        ``} ``        ``return` `rev_num; ``    ``} `` ` `    ``// Driver code ``    ``public` `static` `void` `main(String[] args) ``    ``{ ``        ``int` `num = ``4562``; ``        ``System.out.println(``"Reverse of no. is "``                           ``+ reverseDigits(num)); ``    ``} ``} `` ` `// This code is contributed by Anant Agarwal.`

## Python

 `# Python program to reverse a number `` ` `n ``=` `4562``rev ``=` `0`` ` `while``(n > ``0``): ``    ``a ``=` `n ``%` `10``    ``rev ``=` `rev ``*` `10` `+` `a ``    ``n ``=` `n ``/``/` `10`` ` `print``(rev) `` ` `# This code is contributed by Shariq Raza `

## C#

 `// C# program to reverse a number ``using` `System; `` ` `class` `GFG { ``    ``// Iterative function to ``    ``// reverse digits of num ``    ``static` `int` `reverseDigits(``int` `num) ``    ``{ ``        ``int` `rev_num = 0; ``        ``while` `(num > 0) { ``            ``rev_num = rev_num * 10 + num % 10; ``            ``num = num / 10; ``        ``} ``        ``return` `rev_num; ``    ``} `` ` `    ``// Driver code ``    ``public` `static` `void` `Main() ``    ``{ ``        ``int` `num = 4562; ``        ``Console.Write(``"Reverse of no. is "``                      ``+ reverseDigits(num)); ``    ``} ``} `` ` `// This code is contributed by Sam007`

## PHP

 ` 1) ``    ``{ ``        ``\$rev_num` `= ``\$rev_num` `* 10 +  ``                        ``\$num` `% 10; ``        ``\$num` `= (int)``\$num` `/ 10; ``    ``} ``    ``return` `\$rev_num``; ``} `` ` `// Driver Code ``\$num` `= 4562; ``echo` `"Reverse of no. is "``,  ``       ``reverseDigits(``\$num``); `` ` `// This code is contributed by aj_36 ``?>`

## Javascript

 ``

Output
`Reverse of no. is 2654`

Time Complexity: O(log10 n), where n is the input number.
Auxiliary Space: O(1)

RECURSIVE WAY :

## C

 `// C program to reverse digits of a number ``#include `` ` `/* Recursive function to reverse digits of num*/``int` `reverseDigits(``int` `num) ``{ ``    ``static` `int` `rev_num = 0; ``    ``static` `int` `base_pos = 1; ``    ``if` `(num > 0) { ``        ``reverseDigits(num / 10); ``        ``rev_num += (num % 10) * base_pos; ``        ``base_pos *= 10; ``    ``} ``    ``return` `rev_num; ``} `` ` `/*Driver program to test reverse Digits*/``int` `main() ``{ ``    ``int` `num = 4562; ``    ``printf``(``"Reverse of no. is %d"``, reverseDigits(num)); `` ` `    ``getchar``(); ``    ``return` `0; ``}`

## C++

 `// C++ program to reverse digits of a number ``#include ``using` `namespace` `std; ``/* Recursive function to reverse digits of num*/``int` `reverseDigits(``int` `num) ``{ ``    ``static` `int` `rev_num = 0; ``    ``static` `int` `base_pos = 1; ``    ``if` `(num > 0) { ``        ``reverseDigits(num / 10); ``        ``rev_num += (num % 10) * base_pos; ``        ``base_pos *= 10; ``    ``} ``    ``return` `rev_num; ``} `` ` `// Driver Code ``int` `main() ``{ ``    ``int` `num = 4562; ``    ``cout << ``"Reverse of no. is "` `<< reverseDigits(num); `` ` `    ``return` `0; ``} `` ` `// This code is contributed ``// by Akanksha Rai(Abby_akku)`

## Java

 `// Java program to reverse digits of a number `` ` `// Recursive function to ``// reverse digits of num ``class` `GFG { ``    ``static` `int` `rev_num = ``0``; ``    ``static` `int` `base_pos = ``1``; ``    ``static` `int` `reverseDigits(``int` `num) ``    ``{ ``        ``if` `(num > ``0``) { ``            ``reverseDigits(num / ``10``); ``            ``rev_num += (num % ``10``) * base_pos; ``            ``base_pos *= ``10``; ``        ``} ``        ``return` `rev_num; ``    ``} `` ` `    ``// Driver Code ``    ``public` `static` `void` `main(String[] args) ``    ``{ ``        ``int` `num = ``4562``; ``        ``System.out.println(reverseDigits(num)); ``    ``} ``} `` ` `// This code is contributed by mits`

## Python3

 `# Python 3 program to reverse digits ``# of a number ``rev_num ``=` `0``base_pos ``=` `1`` ` `# Recursive function to reverse ``# digits of num `` ` ` ` `def` `reverseDigits(num): ``    ``global` `rev_num ``    ``global` `base_pos ``    ``if``(num > ``0``): ``        ``reverseDigits((``int``)(num ``/` `10``)) ``        ``rev_num ``+``=` `(num ``%` `10``) ``*` `base_pos ``        ``base_pos ``*``=` `10``    ``return` `rev_num `` ` ` ` `# Driver Code ``num ``=` `4562``print``(``"Reverse of no. is "``, ``      ``reverseDigits(num)) `` ` `# This code is contributed by Rajput-Ji `

## C#

 `// C# program to reverse digits of a number `` ` `// Recursive function to ``// reverse digits of num ``using` `System; ``class` `GFG { ``    ``static` `int` `rev_num = 0; ``    ``static` `int` `base_pos = 1; ``    ``static` `int` `reverseDigits(``int` `num) ``    ``{ ``        ``if` `(num > 0) { ``            ``reverseDigits(num / 10); ``            ``rev_num += (num % 10) * base_pos; ``            ``base_pos *= 10; ``        ``} ``        ``return` `rev_num; ``    ``} `` ` `    ``// Driver Code ``    ``public` `static` `void` `Main() ``    ``{ ``        ``int` `num = 4562; ``        ``Console.WriteLine(reverseDigits(num)); ``    ``} ``} `` ` `// This code is contributed ``// by inder_verma`

## PHP

 ` 0)  ``    ``{  ``        ``reverseDigits((int)(``\$num` `/ 10));  ``        ``\$rev_num` `+= (``\$num` `% 10) *  ``                     ``\$base_pos``;  ``        ``\$base_pos` `*= 10;  ``    ``}  ``    ``return` `\$rev_num``;  ``}  `` ` `// Driver Code ``\$num` `= 4562;  ``echo` `"Reverse of no. is "``,  ``       ``reverseDigits(``\$num``);  `` ` `// This code is contributed by ajit ``?>`

## Javascript

 ``

Output
`Reverse of no. is 2654`

Time Complexity: O(log(n))
Auxiliary Space: O(log(n)),  where n is the input number.

Using Recursion, without extra variable

## C++

 `#include ``#include ``using` `namespace` `std; `` ` `// util functions ``int` `len(``int` `number) ``{ ``    ``// returns the length of a given number ``    ``return` `log10``(number) + 1; ``} `` ` `// reverse a given number ``int` `rev_number(``int``& number) ``{ ``    ``if` `((number % 10) == number) ``        ``return` `number; ``    ``int` `last = number % 10; ``    ``int` `remaining = number / 10; ``    ``int` `l = len(remaining); ``    ``return` `last * ``pow``(10, l) + rev_number(remaining); ``} `` ` `int` `main() ``{ ``    ``int` `number = 123456; ``    ``cout << rev_number(number) << endl; ``    ``return` `0; ``}`

## Java

 `import` `java.lang.Math; ``import` `java.util.*; `` ` `class` `GFG { `` ` `    ``public` `static` `int` `len(``int` `number) ``    ``{ ``        ``// returns the length of a given number ``        ``return` `(``int``)(Math.log10(number)) + ``1``; ``    ``} `` ` `    ``// reverse a given number ``    ``public` `static` `int` `rev_number(``int` `number) ``    ``{ ``        ``if` `(number % ``10` `== number) { ``            ``return` `number; ``        ``} `` ` `        ``int` `last = number % ``10``; ``        ``int` `remaining = number / ``10``; ``        ``int` `l = len(remaining); ``        ``return` `last * (``int``)Math.pow(``10``, l) ``            ``+ rev_number(remaining); ``    ``} `` ` `    ``public` `static` `void` `main(String[] args) ``    ``{ ``        ``int` `number = ``123456``; ``        ``System.out.println(rev_number(number)); ``    ``} ``} `` ` `// This code is contributed by talktoanmol.`

## Python3

 `# util functions ``def` `number_length(num): `` ` `  ``# Return length of given number ``    ``return` `len``(``str``(num)) `` ` `# reverse a given number `` ` ` ` `def` `rev_number(number): ``    ``if` `(number ``%` `10``) ``=``=` `number: ``        ``return` `number `` ` `    ``last ``=` `number ``%` `10``    ``remaining ``=` `number ``/``/` `10``    ``l ``=` `number_length(remaining) `` ` `    ``return` `last``*``pow``(``10``, l) ``+` `rev_number(remaining) `` ` ` ` `def` `main(): ``    ``number ``=` `123456``    ``print``(rev_number(number)) `` ` ` ` `# driver code ``if` `__name__ ``=``=` `"__main__"``: ``    ``main() `` ` `    ``# This code is contributed by talktoanmol `

## C#

 `// C# program to reverse a number ``using` `System; `` ` `public` `class` `GFG { `` ` `    ``public` `static` `int` `length(``int` `num) ``    ``{ ``        ``// returns the length of a given number ``        ``return` `(``int``)Math.Log10(num) + 1; ``    ``} `` ` `    ``// reverse a given number ``    ``static` `int` `rev_number(``int` `num) ``    ``{ ``        ``if` `((num % 10) == num) ``            ``return` `num; ``        ``int` `last = num % 10; ``        ``int` `remaining = num / 10; ``        ``int` `l = length(remaining); ``        ``return` `last * (``int``)Math.Pow(10, l) ``            ``+ rev_number(remaining); ``    ``} `` ` `    ``// Driver code ``    ``static` `public` `void` `Main() ``    ``{ ``        ``int` `num = 123456; ``        ``Console.Write(rev_number(num)); ``    ``} ``} `` ` `// This Code is contributed by Susobhan Akhuli`

## Javascript

 ``

## PHP

 ``

Output
`654321`

Time Complexity: O(log10n) where n is the given input number.
Auxiliary Space: O(log10n) for recursive stack space.

Using String in java

We will convert the number to a string using StringBuffer after this, we will reverse that string using the reverse() method

corner case

Input: 32100

So for the above input if we try to solve this by reversing the string, then the output will be 00123.

So to deal with this situation we again need to convert the string to integer so that our output will be 123

## C

 `// C program to reverse a number ``#include ``#include ``#include `` ` `// reverse string function ``void` `reverse(``char``* begin, ``char``* end) ``{ ``    ``char` `temp; ``    ``while` `(begin < end) { ``        ``temp = *begin; ``        ``*begin++ = *end; ``        ``*end-- = temp; ``    ``} ``} `` ` `// Function to reverse words ``void` `reverseWords(``char``* s) ``{ ``    ``char``* word_begin = s; `` ` `    ``// Word boundary ``    ``char``* temp = s; `` ` `    ``// Reversing individual words as ``    ``// explained in the first step ``    ``while` `(*temp) { ``        ``temp++; ``        ``if` `(*temp == ``'\0'``) { ``            ``reverse(word_begin, temp - 1); ``        ``} ``        ``else` `if` `(*temp == ``' '``) { ``            ``reverse(word_begin, temp - 1); ``            ``word_begin = temp + 1; ``        ``} ``    ``} `` ` `    ``// Reverse the entire string ``    ``reverse(s, temp - 1); ``} `` ` `int` `reverseDigits(``int` `num) ``{ ``    ``char` `strin[100]; ``    ``// converting number to string ``    ``sprintf``(strin, ``"%d"``, num); `` ` `    ``// reversing the string ``    ``reverseWords(strin); `` ` `    ``// converting string to integer ``    ``num = ``atoi``(strin); `` ` `    ``// returning integer ``    ``return` `num; ``} `` ` `int` `main() ``{ ``    ``int` `num = 123456; ``    ``printf``(``"Reverse of no. is %d"``, reverseDigits(num)); ``    ``return` `0; ``} `` ` `// This Code is contributed by Susobhan Akhuli`

## C++

 `// C++ program to reverse a number ``#include ``using` `namespace` `std; `` ` `int` `reverseDigits(``int` `num) ``{ ``    ``// converting number to string ``    ``string strin = to_string(num); `` ` `    ``// reversing the string ``    ``reverse(strin.begin(), strin.end()); `` ` `    ``// converting string to integer ``    ``num = stoi(strin); `` ` `    ``// returning integer ``    ``return` `num; ``} ``int` `main() ``{ ``    ``int` `num = 4562; ``    ``cout << ``"Reverse of no. is "` `<< reverseDigits(num); ``    ``return` `0; ``} `` ` `// This Code is contributed by ShubhamSingh10`

## Java

 `// Java program to reverse a number `` ` `public` `class` `GFG { ``    ``static` `int` `reversDigits(``int` `num) ``    ``{ ``        ``// converting number to string ``        ``StringBuffer string ``            ``= ``new` `StringBuffer(String.valueOf(num)); `` ` `        ``// reversing the string ``        ``string.reverse(); `` ` `        ``// converting string to integer ``        ``num = Integer.parseInt(String.valueOf(string)); `` ` `        ``// returning integer ``        ``return` `num; ``    ``} ``    ``public` `static` `void` `main(String[] args) ``    ``{ ``        ``int` `num = ``4562``; ``        ``System.out.println(``"Reverse of no. is "``                           ``+ reversDigits(num)); ``    ``} ``}`

## Python3

 `# Python 3 program to reverse a number `` ` ` ` `def` `reversDigits(num): `` ` `    ``# converting number to string ``    ``string ``=` `str``(num) `` ` `    ``# reversing the string ``    ``string ``=` `list``(string) ``    ``string.reverse() ``    ``string ``=` `''.join(string) `` ` `    ``# converting string to integer ``    ``num ``=` `int``(string) `` ` `    ``# returning integer ``    ``return` `num `` ` ` ` `# Driver code ``if` `__name__ ``=``=` `"__main__"``: `` ` `    ``num ``=` `4562``    ``print``(``"Reverse of no. is "``, reversDigits(num)) `` ` `    ``# This code is contributed by ukasp. `

## C#

 `// C# program to reverse a number ``using` `System; `` ` `public` `class` `GFG { `` ` `    ``public` `static` `string` `ReverseString(``string` `s) ``    ``{ ``        ``char``[] array = s.ToCharArray(); ``        ``Array.Reverse(array); ``        ``return` `new` `string``(array); ``    ``} `` ` `    ``static` `int` `reversDigits(``int` `num) ``    ``{ ``        ``// converting number to string ``        ``string` `strin = num.ToString(); `` ` `        ``// reversing the string ``        ``strin = ReverseString(strin); `` ` `        ``// converting string to integer ``        ``num = ``int``.Parse(strin); `` ` `        ``// returning integer ``        ``return` `num; ``    ``} `` ` `    ``// Driver code ``    ``static` `public` `void` `Main() ``    ``{ ``        ``int` `num = 4562; ``        ``Console.Write(``"Reverse of no. is "``                      ``+ reversDigits(num)); ``    ``} ``} `` ` `// This Code is contributed by ShubhamSingh10`

## Javascript

 ``

## PHP

 ``

Output
`Reverse of no. is 123456`

Time Complexity: O(log10n) where n is the input number
Auxiliary Space: O(1)

Reverse digits of an integer with overflow handled

Note that the above program doesn’t consider leading zeroes. For example, for 100 programs will print 1. If you want to print 001 then see this comment from Maheshwar.

Using Slicing in Python:

## C++

 `// C++ program for the above approach ``#include ``using` `namespace` `std; `` ` `int` `reverseDigits(``int` `num) ``{ `` ` `    ``// converting number to string ``    ``string str = to_string(num); `` ` `    ``// reversing the string ``    ``reverse(str.begin(), str.end()); `` ` `    ``// converting string to integer ``    ``num = stoll(str); `` ` `    ``// returning integer ``    ``return` `num; ``} `` ` `// Driver Code ``int` `main() ``{ ``    ``int` `num = 4562; ``    ``cout << ``"Reverse of no. is "` `<< reverseDigits(num); `` ` `    ``return` `0; ``} `` ` `// This code is contributed by sanjoy_62.`

## Java

 `// Java program for the above approach `` ` `import` `java.util.*; `` ` `class` `GFG { `` ` `    ``static` `int` `reversDigits(``int` `num) ``    ``{ `` ` `        ``// converting number to string ``        ``String str = String.valueOf(num); `` ` `        ``// reversing the string ``        ``str = ``new` `StringBuilder(str).reverse().toString(); `` ` `        ``// converting string to integer ``        ``num = Integer.valueOf(str); `` ` `        ``// returning integer ``        ``return` `num; ``    ``} `` ` `    ``// Driver Code ``    ``public` `static` `void` `main(String[] args) ``    ``{ ``        ``int` `num = ``4562``; ``        ``System.out.println(``"Reverse of no. is "``                           ``+ reversDigits(num)); ``    ``} ``} `` ` `// This code is contributed by phasing17`

## Python3

 `# Python 3 program to reverse a number `` ` ` ` `def` `reversDigits(num): `` ` `    ``# converting number to string ``    ``string ``=` `str``(num) `` ` `    ``# reversing the string ``    ``string ``=` `string[::``-``1``] `` ` `    ``# converting string to integer ``    ``num ``=` `int``(string) `` ` `    ``# returning integer ``    ``return` `num `` ` ` ` `# Driver code ``if` `__name__ ``=``=` `"__main__"``: `` ` `    ``num ``=` `4562``    ``print``(``"Reverse of no. is "``, reversDigits(num)) `` ` `    ``# This code is contributed by Susobhan Akhuli `

## C#

 `// C# program for the above approach ``using` `System; ``class` `GFG { `` ` `    ``static` `int` `reversDigits(``int` `num) ``    ``{ `` ` `        ``// converting number to string ``        ``string` `str = Convert.ToString(num); `` ` `        ``// reversing the string ``        ``char``[] charArray = str.ToCharArray(); ``        ``Array.Reverse(charArray); ``        ``str = ``new` `string``(charArray); `` ` `        ``// converting string to integer ``        ``num = Convert.ToInt32(str); `` ` `        ``// returning integer ``        ``return` `num; ``    ``} `` ` `    ``// Driver Code ``    ``public` `static` `void` `Main(``string``[] args) ``    ``{ ``        ``int` `num = 4562; ``        ``Console.Write(``"Reverse of no. is "``                      ``+ reversDigits(num)); ``    ``} ``} `` ` `// This code is contributed by phasing17`

## Javascript

 `// JavaScript program for the above approach `` ` `function` `reversDigits(num){ `` ` `  ``// converting number to string ``  ``let str = num.toString().split(``""``); `` ` `  ``// reversing the string ``  ``str.reverse(); ``   ` ` ` `  ``// converting string to integer ``  ``num = parseInt(str.join(``""``)) `` ` `  ``// returning integer ``  ``return` `num; ``} `` ` `// Driver Code ``let num = 4562; ``console.log(``"Reverse of no. is "` `+ reversDigits(num)); `` ` ` ` `// This code is contributed by phasing17`

Output
`Reverse of no. is 2654`

Time Complexity: O(n), where n is the input number
Auxiliary Space: O(1)

Try extensions of above functions that should also work for floating-point numbers.

Divide and Conquer:

Algorithm:

```Input: n
(1) Initialize rev1=0, rev2=0
(2) Compute no of digits in given input n and store it in size variable.
(3) Divide the number n into two parts i.e first_half and second_half.
(4) Loop for i in range(mid):
(a) Remainder of first_half by 10 add it to the multiplication of 10 and rev1.
rem=first_half%10
rev1=10*rev1+rem
(b) Remainder of second_half by 10 add it to the multiplication of 10 and rev2.
rem=second_half%10
rev2=10*rev2+rem
(c) Divide first_half and second_half with 10.
first_half=first_half//10
second_half=second_half//10
(5)if size is even.
return rev2*10**mid+rev1
otherwise
return (rev2*10**mid+rev1)*10+first_half```

## C++

 `#include ``#include ``using` `namespace` `std; ``int` `main() ``{ ``    ``long` `int` `n,first_half,second_half,rem,rev1,rev2,size,mid; ``    ``n=1234; ``    ``rev1=0,rev2=0; `` ` `//Find no of digits in number store it in size and divide size into two parts. ``    ``size=``floor``(``log10``(n)+1); ``    ``mid=size/2; `` ` `//Divide the given number in to two parts. ``    ``first_half=n/``pow``(10,mid); ``    ``second_half=n%(``long` `int``)(``pow``(10,mid)+0.5); `` ` `//Iterate the loop upto half no of digits in number. ``    ``for``(``int` `i=0; i

## Java

 `import` `java.lang.Math; ``class` `Main ``{ ``    ``public` `static` `void` `main (String[] args)  ``    ``{ ``    ``int` `n,first_half,second_half,rem,rev1=``0``,rev2=``0``,size,mid; ``    ``n=``1234``; ``//Find no of digits in number store it in size and divide size into two parts. ``    ``size=(``int``) Math.log10(n)+``1``; ``    ``mid=(``int``) size/``2``; `` ` `//Divide the given number in to two parts. ``    ``first_half=(``int``) (n/Math.pow(``10``,mid)); ``    ``second_half=n%(``int``) (Math.pow(``10``,mid));    ``     ` `//Iterate the loop upto half no of digits in number. ``    ``for``(``int` `i=``0``; i

## Python3

 `# Python program to reverse a number. ``import` `math ``n ``=` `1234``rev1 ``=` `0``rev2 ``=` `0`` ` `# Find no of digits in number store it in size and divide size into two parts. ``size ``=` `int``(math.log10(n))``+``1``mid ``=` `size``/``/``2`` ` `# Divide the given number in to two parts. ``first_half ``=` `n``/``/``10``*``*``mid ``second_half ``=` `n ``%` `10``*``*``mid `` ` `# Iterate the loop upto half no of digits in number. ``for` `i ``in` `range``(mid): ``# Reverse the First half digits and second half digits concurrently ``    ``rem ``=` `first_half ``%` `10``    ``rev1 ``=` `10``*``rev1``+``rem ``    ``rem ``=` `second_half ``%` `10``    ``rev2 ``=` `10``*``rev2``+``rem ``    ``first_half ``=` `first_half``/``/``10``    ``second_half ``=` `second_half``/``/``10``     ` `# Concate Last half with First Half ``print``(``'Original no:'``, n) ``if``(size ``%` `2` `=``=` `0``): ``    ``print``(``'Reversed no:'``, rev2``*``10``*``*``mid``+``rev1) ``else``: ``    ``print``(``'Reversed no:'``, (rev2``*``10``*``*``mid``+``rev1)``*``10``+``first_half) ``     ` `#This code contributed by SR DHANUSH`

## C#

 `// C# program to reverse a number. ``using` `System; `` ` `class` `Program ``{ ``    ``static` `void` `Main(``string``[] args) ``    ``{ ``        ``int` `first_Half, second_Half, rem, rev1 = 0, rev2 = 0, size, mid; ``        ``int` `n = 1234; ``        ``//Find no of digits in number store it in size and divide size into two parts. ``        ``size = (``int``)Math.Log10(n) + 1; ``        ``mid = size / 2; ``         ` `        ``//Divide the given number in to two parts. ``        ``first_Half = (``int``)(n / Math.Pow(10, mid)); ``        ``second_Half = n % (``int``)(Math.Pow(10, mid)); ``         ` `        ``//Iterate the loop upto half no of digits in number. ``        ``for` `(``int` `i = 0; i < mid; i++) ``        ``{ ``            ``//Reverse the First half digits and second half digits concurrently ``            ``rem = first_Half % 10; ``            ``rev1 = 10 * rev1 + rem; ``            ``rem = second_Half % 10; ``            ``rev2 = 10 * rev2 + rem; ``            ``first_Half /= 10; ``            ``second_Half /= 10; ``        ``} `` ` `        ``Console.WriteLine(``"Original no: "` `+ n); ``         ` `        ``//Concate Last half with First Half ``        ``if` `(size % 2 == 0) ``        ``{ ``            ``Console.WriteLine(``"Reversed no: "` `+ (rev2 * (``int``)(Math.Pow(10, mid)) + rev1)); ``        ``} ``        ``else``        ``{ ``            ``Console.WriteLine(``"Reversed no: "` `+ ((rev2 * (``int``)(Math.Pow(10, mid) + 0.5) + rev1) * 10 + first_Half)); ``        ``} ``    ``} ``} `` ` `// This code is contributed by Aman Kumar`

## Javascript

 `// JS code to implement the approach `` ` `// Find the number of digits in the number and divide it in ``// half ``let n = 1234; ``let rev1 = 0; ``let rev2 = 0; ``let size = Math.floor(Math.log10(n)) + 1; ``let mid = Math.floor(size / 2); `` ` `// Divide the number into two parts ``let firstHalf = Math.floor(n / 10 ** mid); ``let secondHalf = n % 10 ** mid; `` ` `// Reverse the first half and second half digits ``// concurrently ``for` `(let i = 0; i < mid; i++) { ``    ``let rem = firstHalf % 10; ``    ``rev1 = rev1 * 10 + rem; ``    ``let rem2 = secondHalf % 10; ``    ``rev2 = rev2 * 10 + rem2; ``    ``firstHalf = Math.floor(firstHalf / 10); ``    ``secondHalf = Math.floor(secondHalf / 10); ``} `` ` `// Concatenate the last half with the first half ``console.log(``"Original no:"``, n); ``if` `(size % 2 == 0) { ``    ``console.log(``"Reversed no:"``, rev2 * 10 ** mid + rev1); ``} ``else` `{ ``    ``console.log(``"Reversed no:"``, ``                ``(rev2 * 10 ** mid + rev1) * 10 + firstHalf); ``} `` ` `// This code is contributed by phasing17`

Output
```Original no: 1234
Reversed no: 4321```

Time Complexity: O(log10(half no of digits in number)), the number indicates given input.
Auxiliary Space: O(1).

using divide and conquer:

We will divide the given number into two parts after this, reverse the first half and second half and join them.

Previous
Next