Program to reverse an integer using a function

In this python function program, we will reverse an integer using a function.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function reverse.
  2. Use the def keyword to define the function.
  3. Take a number as input through the user and create a variable rev and assign its value equal to 0.
  4. Use a while loop to find the reverse of a number.
  5. While num is greater than 0 divide the number by 10 using % to find the remainder.
  6. Multiply the rev by 10 and add the remainder to it.
  7. Now divide number by 10 using //.
  8. Print the final output.
  9. Call the function to see the output.
				
					def reverse():
    num=n=int(input("Enter a number: "))
    rev=0
    while n>0:
        r=n%10
        rev=(rev*10)+r
        n=n//10
    print("Reverse number: ",rev)
reverse()
				
			

Output :

				
					Enter a number: 225
Reverse number:  522
				
			

Related Articles

check whether the given year is a leap year.

create a library management system.

Leave a Comment