Reverse a given number using Java
Reverse of a number in java :
In this article, we will create a java program to reverse a number. Here we will ask the user to enter a number and then by the help of the java code we will reverse the number and display it on the screen.
Reversing of a number generally means printing the number in reverse order.
On most of the sites, the reverse of a number is done in wrong way because if you put 90, 100, 1300 and many other inputs which have ‘ 0 ’ in last digits of the number then it will neglect 0 in their output. So, below mentioned code is made after considering all the cases.
For better understanding let’s consider some examples :
- 1900 : Reverse is 0091
- 95724 : Reverse is 42759
Step 1 : Ask the user to enter a number.
Step 2 : Declare a string variable to store reversed number.
Step 3 : Use a loop to perform continuous iterations till the number entered by user becomes 0.
Step 4 : Pick every last digit of the number at each iteration and store it in a variable pick_last.
Step 5 : Use a statement to reverse the number.
Step 6 : Change the value of number by removing the last digit which is stored in the pick_last variable.
Step 7 : Repeat the Step 4 to 6 till the condition of step 3 gets false.
Step 8 : Display the reversed string.
Code in Java :
//Java program to print reverse of a number
public class reverse_of_number
public static void main(String args)
//scanner class declaration
Scanner sc = new Scanner(System.in);
//input from user
System.out.print("Enter a number : ");
int number = sc.nextInt();
System.out.print("Reverse of "+number+" is ");
int reverse = 0;
String s = "";
while(number != 0)
int pick_last = number % 10;
//use function to convert pick_last from integer to string
s = s + Integer.toString(pick_last);
number = number / 10;
//display the reversed number
//closing scanner class(not compulsory, but good practice)
Enter a number : 123
Reverse of 123 is 321
Enter a number : 9000
Reverse of 9000 is 0009
- Positive or Negative number: C | C++ | Java
- Even or Odd number: C | C++ | Java
- Sum of First N Natural numbers: C | C++ | Java
- Sum of N natural numbers: C | C++ | Java
- Sum of numbers in a given range: C | C++ | Java
- Greatest of two numbers: C | C++ | Java
- Greatest of the Three numbers: C | C++ | Java
- Leap year or not: C | C++ | Java
- Prime number: C | C++ | Java
- Prime number within a given range: C | C++ | Java
- Factorial of a number: C | C++ | Java
- Sum of digits of a number: C | C++ | Java
- Reverse of a number : C | C++ | Java
- Palindrome number: C | C++ | Java
- Armstrong number : C | C++ | Java
- Armstrong number in a given range : C | C++ | Java
- Fibonacci Series upto nth term : C | C++ | Java
- Factorial of a number : C | C++ | Java
- Power of a number : C | C++ | Java
- Factor of a number : C | C++ | Java
- Strong number : C | C++ | Java
- Perfect number : C | C++ | Java
- Automorphic number : C | C++ | Java
- Harshad number : C | C++ | Java
- Abundant number : C| C++ | Java
- Friendly pair : C | C++ | Java