Reverse a given number – Java Code

Objective: Given a number, write a java program to reverse a number

Example:

Given Number: 1234
Output: 4321
Given Number: 1020
Output: 201

Approach:  Using loop

  • Keep divide the number by 10 and add the remainder to the reversed number (after multiple reversed number with 10)
  • See the code.

Java Code:

Output:

Given Number: 1234
Reversed Number: 4321
Given Number: 1020
Reversed Number: 201

Approach: Using Recursion

  • Divide the number by 10 and add the remainder to the reversed number (after multiple reversed number with 10) and make a recursive call with number and reversed number.
  • See the code.

Java Code:

Output:

Given Number: 1234
Reversed Number using recursion: 4321
Given Number: 1020
Reversed Number using recursion: 201

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

%d bloggers like this: