Python Program To Reverse a Number (3 Ways)

Python Program To Reverse a Number

In this tutorial, you will learn to write a Python Program To Reverse a Number.

We will discuss three different ways to do this.

To understand this program you should have an understanding of the following topics:

  1. While loop
  2. For Loop
  3. Python Lists

Python Program To Reverse a Number Using While Loop

num = int(input("Enter a number: "))

reverse = 0
while num > 0:
    digit = num % 10
    reverse = reverse * 10 + digit
    num = num // 10

print("The reversed number is:", reverse)

Output

Enter a number: 12345
The reversed number is: 54321

In this program, we first prompt the user to input a number.

We then declare a variable reverse and initialize it to 0.

We then enter a while loop, which continues until num becomes 0.

In each iteration of the loop, we find the rightmost digit of num by using the modulus operator %.

We add this digit to reverse by multiplying reverse by 10 and adding the digit.

This “shifts” the digits of reverse one place to the left and adds the new digit on the right.

We then remove the rightmost digit from num by using integer division //.

This is equivalent to “shifting” the digits of num one place to the right and removing the rightmost digit.

We repeat this process until num becomes 0.

Once the loop finishes, we print out the final value of reverse, which is the reversed number.

Python Program To Reverse a Number Using For Loop

num = int(input("Enter a number: "))
num_str = str(num)

reverse_str = ""
for i in range(len(num_str)-1, -1, -1):
    reverse_str += num_str[i]

reverse_num = int(reverse_str)

print("The reversed number is:", reverse_num)

Output

Enter a number: 456789
The reversed number is: 987654

In this program, we first prompt the user to input a number.

We then convert the number to a string using str(num) and store it in a variable called num_str.

We then declare an empty string called reverse_str.

We use a for loop to iterate over the characters of num_str in reverse order.

We use the range() function to generate a sequence of indices that start at the last index of num_str and go down to the first index.

Inside the loop, we append each character of num_str to reverse_str in reverse order by using the += operator.

Once the loop finishes, we have the reverse of the number as a string.

We convert it back to an integer using int(reverse_str) and store it in a variable called reverse_num.

Finally, we print out the value of reverse_num, which is the reversed number.

Python Program To Reverse a Number Using List Slicing

num = int(input("Enter a number: "))
num_str = str(num)

reverse_str = num_str[::-1]
reverse_num = int(reverse_str)

print("The reversed number is:", reverse_num)

Output

Enter a number: 12789
The reversed number is: 98721

We first prompt the user to input a number.

We then convert the number to a string using str(num) and store it in a variable called num_str.

We then use list slicing to reverse the string.

The syntax for list slicing is start:stop:step.

In this case, we set to start to -1, which means we start at the last character of num_str.

We set stop to None, which means we go up to the first character of num_str.

We set step to -1, which means we iterate backward over the string.

We then store the reversed string in a variable called reverse_str.

Finally, we convert the reversed string back to an integer using int(reverse_str) and store it in a variable called reverse_num.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x