A program to reverse a given number easy steps | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

A program to reverse a given number easy steps

6th Sep 2018, 1:44 PM
Arvind Kumar Singh
Arvind Kumar Singh - avatar
4 Answers
+ 2
There are 4 things you need to understand to do this: - n%10 gives you the ones column of any number, - n/10 shifts the tens column and beyond down to the ones column, - n*10 shifts the ones column and beyond up to the tens column, and - n+d puts a digit d into the ones column assuming it is empty. Such as: 23%10=3 23/10=2 3*10=30 30+2=32 Put together correctly in a loop until the original number is zero and the result is a reversed number.
6th Sep 2018, 2:25 PM
John Wells
John Wells - avatar
+ 1
class Reverse { public static void main(int n) { int s=0; while(n!=0) { s=s*10+(n%10); n/=10; } System.out.println(s); } }
6th Sep 2018, 2:18 PM
Varun Maheshwari
Varun Maheshwari - avatar
+ 1
Thanks a lot John for helping me..
6th Sep 2018, 2:26 PM
Arvind Kumar Singh
Arvind Kumar Singh - avatar
0
Thanks Varun..
6th Sep 2018, 2:19 PM
Arvind Kumar Singh
Arvind Kumar Singh - avatar