Java program to reverse a number

In this post, we will see how to reverse a number in java.

Let’s create a java program which will reverse a number.

When you run above program, you will get below output:

Enter the number to reverse
Reverse of entered number is 65432

Let’s understand the program.

  • Initialize a int variable reverse with 0.This will store our result
  • Iterate over while loop until number!=0
  • Find remainder of number when you divide it by 10
  • Multiply reverse by 10 and add it to the remainder
  • Divide number by 10 to remove most significant digit

That’s all about reversing a number in java.

Leave a Reply

Your email address will not be published. Required fields are marked *