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
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
+ 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.
+ 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.
+ 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???
0
ahhh I see thanks a lot bruh
0
excellent bruh I fully understand it now.....basically the code wasn't complex but the math done inside it was
0
shouldnt it be S.reverse
0
if we use S.reverse and A.reverse it is working