Showing posts with label Write a program to an integer as an input and reverse that integer.. Show all posts
Showing posts with label Write a program to an integer as an input and reverse that integer.. Show all posts
Sunday, April 9, 2023

Write a program to an integer as an input and reverse that integer.

 


Write a program to an integer as an input and reverse that integer.


Input:
A single integer.

Output:
Reverse number of that integer.

Example:

Input:
54321

Output:
12345

Python Code With Explanation:

#The program takes an integer input from the user using the input() function
#and converts it into an integer using the int() function.
num = int(input())

#We initialize a variable reverse to 0 to store the reverse of the input integer.
reverse = 0

#We use a while loop to extract each digit of the input integer starting from the least significant digit 
#(i.e., the digit in the ones place) 
#and add it to the reverse variable after shifting its existing digits by one place to the left.
while num > 0:
    digit = num % 10
    reverse = (reverse * 10) + digit
    
#We update the input integer by removing the least significant digit using integer division (//).
#The loop continues until all digits of the input integer have been processed.
    num //= 10

# print the value of reverse which contains the reverse of the input integer.
print(reverse,end="")