Write a Python program to reverse a given number. The program will take a number as input and return the reversed number as output.
Input-Output Examples
plaintext
Example 1:
Input: Enter a number: 12345
Output: Reversed number is: 54321
Example 2:
Input: Enter a number: 9876
Output: Reversed number is: 6789
Algorithm to reverse a given number
- Start
- Take input: Prompt the user to enter a number.
- Handle negative numbers: If the number is negative, remember the sign and work with the absolute value.
- Reverse the number using a loop:
- Initialize
reversed_num
to 0. - Extract the last digit of the number using the modulus operator.
- Append the last digit to
reversed_num
. - Remove the last digit from the original number using integer division.
- Repeat the process until the original number becomes 0.
- Initialize
- Add back the negative sign (if necessary).
- Display the result: Print the reversed number.
- End
Python Program to reverse a given number (Using Loop)
python
# Python program to reverse a number using a loop
# Take input from the user
num = int(input("Enter a number: "))
# Initialize variable to store the reversed number
reversed_num = 0
# Handle negative numbers by storing the sign
sign = 1
if num < 0:
sign = -1
num = abs(num)
# Loop to reverse the number
while num > 0:
last_digit = num % 10 # Get the last digit
reversed_num = reversed_num * 10 + last_digit # Append last digit
num //= 10 # Remove the last digit from num
# Add back the negative sign if necessary
reversed_num *= sign
# Display the reversed number
print(f"Reversed number is: {reversed_num}")
Python Program to reverse a given number (Using String Conversion)
python
# Python program to reverse a number using string conversion
# Take input from the user
num = input("Enter a number: ")
# Reverse the string and handle negative numbers
if num[0] == '-':
reversed_num = '-' + num[:0:-1] # Reverse excluding the negative sign
else:
reversed_num = num[::-1] # Reverse the string
# Display the reversed number
print(f"Reversed number is: {reversed_num}")
Code Explanation
- Taking input from the user:
The program prompts the user to enter a number usinginput()
. In the first method, the input is converted to an integer, while in the second method, it is treated as a string. - Handling negative numbers:
- In the first approach (using loops), if the input is negative, the sign is stored, and the absolute value of the number is processed. After reversing, the negative sign is added back to the result.
- In the second approach (using string slicing), if the input is negative, the negative sign is handled separately while reversing the rest of the digits.
- Reversing the number using a loop:
The program uses awhile
loop to reverse the number by extracting the last digit (num % 10
) and appending it toreversed_num
. The last digit is removed from the original number using integer division (num //= 10
) until the number becomes 0. - Reversing the number using string slicing:
In the second approach, the number is treated as a string, and string slicing ([::-1]
) is used to reverse the digits. - Displaying the output:
Theprint()
function is used to display the reversed number.