Skip to content

Instantly share code, notes, and snippets.

@hossainlab
Created April 12, 2019 15:37
Show Gist options
  • Select an option

  • Save hossainlab/b58bd9b22dfeb5eeedba4ee37c007946 to your computer and use it in GitHub Desktop.

Select an option

Save hossainlab/b58bd9b22dfeb5eeedba4ee37c007946 to your computer and use it in GitHub Desktop.
Python Program To Reverse a Given Number

Python Program to Reverse a Given Number

n = int(input("Enter a number: "))
reversed_number = 0
while(n>0):
    last_digit = n%10
    reversed_number = reversed_number*10+last_digit
    n = n//10
print("The reversed number is: ",reversed_number)

Explanation

  • Take input from user and store in a variable called n
  • Set the counter, which is reverse_number= 0
  • Check the n is greater than 0
  • Get the last digit of entered number by using modulus operator(%)
  • Store the last digit in reversed_number variable
  • After storing the last digit,it should be removed.So remove the last digit by using floor division operator
  • Loop terminates when the value of n is 0
  • Print the reversed number
@naivaidyaneha-del

Copy link
Copy Markdown

N=124

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment