Can someone explain this code to me.... 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); } } the thing I don't get is the reversenum part......isn't reversenum=0 so wouldn't reversenum*10=0 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone explain this code to me.... 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); } } the thing I don't get is the reversenum part......isn't reversenum=0 so wouldn't reversenum*10=0

25th Jun 2016, 8:45 PM
Arsal Azam
Arsal Azam - avatar
8 Answers
+ 2
hey Arzal ur thinking is little bit innocent. in ur code reversenum is initizalized and declare to 0 is correct.after the zero operation it will add the given number of the last digits.hence process is going to till upto num=0; while loop false it will terminate that time and u willget new reversevalue
18th Sep 2016, 6:53 PM
Pranit Bhoir
Pranit Bhoir - avatar
+ 1
At first the reversenum when multiplied by 10 will be zero......but after that 9 will be added to it.Now, when the loop will be started again...the reversenum will be 9*10 and this will continue until the condition is justified.
25th Jun 2016, 10:57 PM
Fatima Qazi
Fatima Qazi - avatar
+ 1
1)any % 10 》》lastdigit ex: 1234 % 10 》》4 2) anynumber /10 》》remove last digit ex: 1234 /10 》》 123 hence this is a logic , till yur while loop terminates.
25th Jun 2016, 11:09 PM
Aamir Memon
Aamir Memon - avatar
+ 1
public class ReverseString{ public static void main(String args[]){ StringBuffer S = new StringBuffer("Make it reverse"); System.out.println(a.reverse()); } } if you run this program you will get reversed string. but instead of it i hav given integers too im getting reversed numbers eg i hav given 123....my output is 321 is it correct???
27th Jun 2016, 10:41 AM
Murali Malayathri
Murali Malayathri - avatar
0
ahhh I see thanks a lot bruh
25th Jun 2016, 11:01 PM
Arsal Azam
Arsal Azam - avatar
0
excellent bruh I fully understand it now.....basically the code wasn't complex but the math done inside it was
25th Jun 2016, 11:14 PM
Arsal Azam
Arsal Azam - avatar
0
shouldnt it be S.reverse
27th Jun 2016, 10:48 AM
Arsal Azam
Arsal Azam - avatar
0
if we use S.reverse and A.reverse it is working
31st May 2018, 1:32 AM
sreekanth s