Write the python code to input a number and print the reverse of the number by using for loop (not while or any other loop)
Answers
Answer:
Python Program
Python Programtry: n = int(input('Enter a number : ')) reversed = 0 while(n!= 0): r=int(n%10) reversed = reversed*10 + r n=int(n/10) print(reversed) except ValueError: print('Given input is not a number. ')
Explanation:
this is a code to Write the python code to input a number and print the reverse of the number by using for loop.
Question:-
Write a Python code to input a number and print the reverse of the number using for loop.
Program:-
Here is your program written in python.
------------------------------------
n=int(input("Enter a number: ");
s=0
string=str(n)
x=len(string)
for I in range (0,x, 1):
d=n%10
s=s*10+d
n=n//10
if n==0:
break
print("The reversed number is: ",s)
------------------------------------
First of all, we will calculate the number of digits of the number(say n) . Then a loop will run(for n times) that will reverse the number.
After reversing, the number is printed.
But if the user enter 100 then the output is 1(not 001)