Reversing a number

Question:-

Write a function intreverse(n) that takes as input a positive integer n and returns the integer obtained by reversing the digits in n.

Here are some examples of how your function should work.


>>> intreverse(783)

387
>>> intreverse(242789)
987242
>>> intreverse(3)
3

Solution:-




def intreverse(n):
  a=""
  while n>0:
    b=str(n%10)
    a=a+b
    n=n//10
  return int(a)

Comments

Popular posts from this blog

Prime partitioning a number

Rotating a list

Alternating prime and square