There are three ways to reverse a number in Java.
1) Using while loop
2) Using for loop
3) Using recursion
4) Reverse the number without user interaction
Program 1: Reverse a number using while Loop
The program will prompt user to input the number and then it will reverse the same number using while loop.
import java.util.Scanner; class ReverseNumberWhile { public static void main(String args[]) { int num=0; int reversenum =0; System.out.println("Input your number and press enter: "); //This statement will capture the user input Scanner in = new Scanner(System.in); //Captured input would be stored in number num num = in.nextInt(); //While Loop: Logic to find out the reverse number while( num != 0 ) { reversenum = reversenum * 10; reversenum = reversenum + num%10; num = num/10; } System.out.println("Reverse of input number is: "+reversenum); } }
Output:
Input your number and press enter: 145689 Reverse of input number is: 986541
Program 2: Reverse a number using for Loop
import java.util.Scanner; class ForLoopReverseDemo { public static void main(String args[]) { int num=0; int reversenum =0; System.out.println("Input your number and press enter: "); //This statement will capture the user input Scanner in = new Scanner(System.in); //Captured input would be stored in number num num = in.nextInt(); /* for loop: No initialization part as num is already * initialized and no increment/decrement part as logic * num = num/10 already decrements the value of num */ for( ;num != 0; ) { reversenum = reversenum * 10; reversenum = reversenum + num%10; num = num/10; } System.out.println("Reverse of specified number is: "+reversenum); } }
Output:
Input your number and press enter: 56789111 Reverse of specified number is: 11198765
Program 3: Reverse a number using recursion
import java.util.Scanner; class RecursionReverseDemo { //A method for reverse public static void reverseMethod(int number) { if (number < 10) { System.out.println(number); return; } else { System.out.print(number % 10); //Method is calling itself: recursion reverseMethod(number/10); } } public static void main(String args[]) { int num=0; System.out.println("Input your number and press enter: "); Scanner in = new Scanner(System.in); num = in.nextInt(); System.out.print("Reverse of the input number is:"); reverseMethod(num); System.out.println(); } }
Output:
Input your number and press enter: 5678901 Reverse of the input number is:1098765
Example: Reverse an already initialized number
In all the above programs we are prompting user for the input number, however if do not want the user interaction part and want to reverse an initialized number then this is how you can do it.
class ReverseNumberDemo { public static void main(String args[]) { int num=123456789; int reversenum =0; while( num != 0 ) { reversenum = reversenum * 10; reversenum = reversenum + num%10; num = num/10; } System.out.println("Reverse of specified number is: "+reversenum); } }
Output:
Reverse of specified number is: 987654321
Reverse number i want explanation mean in layman term ….plz explain the concept of all logic that it happening in middle of program plz explain like u are not writing a program instead your are writing that all math sum in a paper ….
Hi what does it mean when ” int num=0 “? Does that mean all integers start from 0?
by int num=0; it says that not garbage velue(a value which affects the outcome of a result) will be stored in the variable num
int num=0; means that the variable is initialized with the value zero(0).
No…here num is a variable and it is assigned as value 0 which mean num has null value so that it can be used further
In Reverse a number using recursion method:
It is not suitable numbers. bez it is failed the test case at
Input as :: 1200 or zero has last digit.
Please correct me if i am wrong.
Thanks
Praveen
How do i use reverse of a number concept in this palindrome program.. Please help me use it…
@ Saket
Possible explanation is:
Let x=123, then ⌊log10x⌋=2. As ⌊x10−1⌋=⌊12.3⌋=12 and ⌊x10−2⌋=⌊1.23⌋=1, we have
123×102−99(12×10+1×1)=123×100−99×121=12300−11979=321
Hello
can anyone tell me how to reverse a number which starts with zero..like 0213,0334
Can someone please help me how to print this in outpu
987654321
87654321
7654321
654321
54321
4321
321
21
1
public static void printNumbers(int numbers)
{
String strNum = Integer.toString(numbers);
for (int i = 0; i < strNum.length(); i++)
{
System.out.println(strNum.substring(i));
}
}
public static void main(String[] args)
{
printNumbers(987654321);
}
if you want to reverse a number which starts with zero,you should use string data type, as input of 0212 in int data type will takes as 212.
How do you add a max string length to the reverse number program? For example, the number must be positive and have a maximum of six digits.
Hi,
I need an help from you .
Question is below :
Consider that you have two ArrayList first ArrayList takes only Strings , and second ArrayList takes only Integer.
Now I want this two Array list to added to HashMap and iterate it also .
So how would you do ?
And Thanks for all your Programs and concepts that you published.
Your concepts are very much good , informative and quite easy to understand.
Really great website to start with! ^_^
Why is it that reversenum isn’t always zero? The way I’m reading, say we input 205. reversenum = 0 * 10
Reversenum= 0 + 5 = 5
num = 205/10 = 20
So printing reversenum would just print 5? I don’t get that.
Hello
i am having trouble understanding these lines of code.
Could please explain how it works?
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}
Suppose u want to reverse a number 123.
As we have initialized reversenum=0
so,
reversenum = 0 * 10;
reversenum = 0 + 123%10; [123%10=3. so reversenum becomes 3]
num = 123/10;[num stores the value as 12]
In the next iteration,
reversenum = 3* 10;
reversenum = 30 + 12%10;[reversenum=30+2=32.]
num = 12/10;[num stores the value 1]
In the last iteration
reversenum = 32 * 10;
reversenum = 320 + 1%10;[reversenum=320+1=321]
num = 1/10;
Here the looping stops and the result is printed as 321.
executes a block of statements repeatedly until the condition(Boolean expression) returns false, so we choose a number like 325
int num=0;
int reversenum =0;
while( num != 0 )
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}
the step
1. num =325 != 0
2. reversenum = 0*10=0
3. reversenum = 0+(325%10) = 0+5=5
4. num = 325/10 = 32
5. num =32 !=0
6. reversenum = 5 *10 = 50
7. reversenum = 50 + (32 % 10) = 50+2=52
8. num=32/10 = 3
9 num = 3 != 0
10. reversenum = 52 *10 =520
11. reversenum = 520 + (3 % 10) = 520+3 = 523
12. num = 3/10 =0
final num =0 return false, so we get the reverse number 523.
Finally, Now i was able to understand the concept
Can anyone write a program that will accept number and produces sequence number in alternate arrangement and reverse order.
Enter number: 5
Output
5142332415
why did you multiply reversenum by 10
hello Sir I am facing a problem with the logic.The output of 012 is not coming as 210 only 21! Why?