Computer Science, asked by soapmactavish2156, 9 months ago

Write a Python script to read an integer > 1000 and reverse the number.

Answers

Answered by rajgraveiens
1

Answer:

def reverse_num():

   rev = 0

   n =int(input("Please enter any Number greater than 1000: "))

   if n > 1000:

       while(n > 0):

           a = n % 10

           rev = rev * 10 + a  

           n = n // 10

       print("Reverse of entered number is = ",rev)

   else:

       reverse_num()

reverse_num()

Explanation:

Algorithm for finding reverse is given below: -

Input:  enter number greater than 1000, if number is not greater than 1000, enter it again.

(1) Initialize reverse_number(rev) = 0

(2) Loop while number(n) > 0

    (a) Multiply reverse_number by 10 and add remainder of number  

         divide by 10 to reverse_number

              reverse_number = reverse_number*10 + number%10;

    (b) Divide number by 10

(3) Return reverse_number

Example:

number = 1234

reverse_number = 0

reverse_number = reverse_number *10 + number%10 = 4

number = number/10 = 123

reverse_number = reverse_number *10 + number%10 = 40 + 3 = 43

number = number/10 = 12

reverse_number = reverse_number *10 + number%10 = 430 + 2 = 432

number = number/10 =1

reverse_number = reverse_number *10 + number%10 = 4320 + 1 = 4321

number = number/10 = 0

Similar questions