0
A program to reverse a given number easy steps
4 Respuestas
+ 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.
+ 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);
}
}
+ 1
Thanks a lot John for helping me..
0
Thanks Varun..