Basic Programs

If Else Programs

While loop Programs

For loop Programs

List Programs


Python Program to Reverse a number in given n Digit Number using while Loop


In this example shows, how to reverse a digits number in given n digit number using whileloop.

Steps
  1. Get the input from user.
  2. Use modulo to extract the digits from the number.
  3. Divide to shorten the number after the digit has been extracted.
Example:

Example.py
a = int(input("Enter the Value :"))
rev = 0

while(a != 0):
    count = a % 10
    rev = rev * 10 + count
    a = a//10

print("Reversed Number is : " + str(rev))

Output

Enter the Value :123456
Reversed Number is : 654321

Prev Next