Given a number num, you need to write a Python Program to print the number in reverse.
Example: Input: num = 1372 Output: 2731 Input: num = 901 Output: 109
There are multiple approaches to solving this problem, let's discuss each of them one by one:
Reverse a number using String Slicing in Python.
Python slicing technique is one important feature that is used for extracting a portion of a string, a list of tuples. We can use this notation to reverse the given number.
Steps to do so:
- The function takes the input num and converts it to a string.
- The function then uses slicing to reverse the string and returns the result as an integer.
- The program then takes input from the user and stores it in the num variable.
Python Code:
def reverse_number(num): return int(str(num)[::-1]) # take input from the user num = int(input("Enter a number: ")) # reverse the number rev = reverse_number(num) # print the reversed number print("The reversed number is", rev)
Enter a number: 1348
The reversed number is 8431
Reverse a number using a while loop in Python.
We can use any loop to iterate through each digit of the given number and perform some mathematical operations to reverse their order and generate a new reverse number.
Steps to do so:
- The program defines a function reverse_number to reverse a given number.
- The function initializes two variables rev and rem to store the reversed number and the remainder of the number.
- The function then uses a while loop to reverse the number digit by digit.
- In each iteration, the function calculates the remainder of the number by dividing it by 10 and stores it in rem.
- The function then multiplies the current value of rev by 10 and adds rem to it.
- The function then updates the value of num by dividing it by 10.
- The function repeats these steps until num is 0.
Python Code:
def reverse_number(num): rev = 0 while num > 0: rem = num % 10 rev = rev * 10 + rem num = num // 10 return rev # take input from the user num = int(input("Enter a number: ")) # reverse the number rev = reverse_number(num) # print the reversed number print("The reversed number is", rev)
Output:
Enter a number: 034
The reversed number is 43
Reverse a number using Recursion in Python.
This approach is the same as the previous one, In this approach, we are using recursion to iterate through each digit of the given number.
Steps to do so:
- The function takes two inputs: num and rev, where num is the number to be reversed and rev is the reversed number so far.
- The function uses an if statement to check if num is 0. If num is 0, the function returns rev.
- If num is not 0, the function calculates the remainder of num by dividing it by 10 and stores it in rem.
- The function then calls itself with num // 10 and rev * 10 + rem as inputs and returns the result.
Python Code:
def reverse_number(num, rev=0): if num == 0: return rev rem = num % 10 return reverse_number(num // 10, rev * 10 + rem) # take input from the user num = int(input("Enter a number: ")) # reverse the number rev = reverse_number(num) # print the reversed number print("The reversed number is", rev)
Output:
Enter a number: 2034
The reversed number is 4302
- Time Complexity: O(n) where n is the number of digits that the number contains.
- Space Complexity: O(n) stack space is required for performing recursion.
No comments:
Post a Comment