This post is completed by 1 user

  • 1
Add to List
Beginner

353. Reverse a given number

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.

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.

Output:

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