Java program to reverse a number

How to reverse a number?

Take a number “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

Java Program to reverse a number:

OUTPUT:

Let’s dry run an example:

 

There is another way of reversing a number using Recursion.

Lets see how to implement a java program to reverse a number using recursion.

PROGRAM:

 

OUTPUT:

 

Be the first to comment

Leave a Reply