-4

This is the logic to reverse a number. But this logic doesn't reverse the number having 0. for example i want to reverse 70 means it will give output as 7. so kindly give logic to reverse number having 0. Thanks.

while(num>0)
        {
            rem=num%10;
            sum=(sum*10)+rem;
            num=num/10;
        }
        System.out.println("Output is:"+sum );

1 Answers1

0

Integer numbers cannot have leading zeroes.

What would be the use case in which such a function be useful ?

If the purpose is just to reverse an integer number and use it as a string, then this should do the job.

int num = 70 ;
String numStr = new Integer(num).toString();

StringBuilder numStrB = new StringBuilder(numStr);
String reversedStr = numStrB.reverse().toString();
Venu
  • 1
  • 1