# Interview Questions

Ritambhara Technologies - Coding Interview Preparations > Interview Questions > Algorithms > Helper Function: Reverse a Number

# Helper Function: Reverse a Number

- April 26, 2016
- Posted by: Kamal Rawat
- Category: Algorithms

No Comments

Sometimes we need helper functions in our code. One such helper function needed is to reverse a number. i.e a function which returns me ‘321’ if I pass ‘123’ to it. Implement the following function

int reverseNum(int num)

The function should accept an integer and should return an integer whose digits are reverse of the original number.

Input: 123 Output: 321 Input: 0 Output: 0 Input: 100 Output: 1

Note that the reverse of ‘100’ is ‘1’ because we are dealing with numbers. Had it been strings, the reverse of ‘100’ would have been ‘001’.

### Solution:

The code is very simple as shown below.

int reverseNum(int num) { int reverse = 0; while(num != 0) { reverse = reverse*10 + num%10; num = num/10; } return reverse; }