Problem Description
The program takes a number and reverses it.
Problem Solution
1. Take the value of the integer and store in a variable.
2. Using a while loop, get each digit of the number and store the reversed number in another variable.
3. Print the reverse of the number.
4. Exit.
Program/Source Code
Here is the source code of the Python Program to reverse a given number.
n=int(input("Enter number: "))
rev=0
while(n greater than 0):
dig=n%10
rev=rev*10+dig
n=n//10
print("Reverse of the number:",rev)
Program Explanation
1. User must first enter the value and store it in a variable n.
2. The while loop is used and the last digit of the number is obtained by using the modulus operator.
3. The last digit is then stored at the one’s place, second last at the ten’s place and so on.
4. The last digit is then removed by truly dividing the number with 10.
5. This loop terminates when the value of the number is 0.
6. The reverse of the number is then printed.
Смотрите видео Python program to reverse a number | How to check if a number is palindrome or not in Python онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Engineers Revolution 21 Апрель 2020, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 703 раз и оно понравилось 15 людям.