Page 248 - Computer Science Class 11 With Functions
P. 248

10.4.2 A Number with Digits in Reverse Order

        Next, let's develop a function called reverseDigits(num) that takes a non-negative integer as an argument and
        returns a number whose digits are in the opposite order of the argument's digits. For example,  reverseDigits(1234)
        will return 4321. Before we jump into the coding task, let us understand the process of obtaining a number (say,
        reverseNum) whose digits are in the opposite order of the argument's digits. Note that 1234 = ((1x10 + 2)x10 + 3)
        x10+4. So, to build reverseNum, we must compute ((4x10 + 3)x10 + 2)x10+1. For this purpose, we begin with a

        clean slate, i.e., we initialise reverseNum  as 0. Next, we extract the unit's digit (4) and add it to reverseNum.
        So, reverseNum becomes 4. Now that we do not require the unit's digit any more, we discard it by setting num =
        num // 10. Now num is equal to 123. Again, we multiply reverseNum by 10 and add to it the unit's digit (3) from
        current value of num (123). Thus, reverseNum becomes 43. Again, discarding the units's digit of num, we get num
        equal to 12. We multiply reverseNum by 10 and add to it the unit's digit (2) from current value of num (12). Thus,
        reverseNum becomes 432. Now, discarding the units's digit (2) of num (12), we get the num equal to 1. Again, we
        multiply reverseNum by 10 and add to it the unit's digit (1) from current value of num  (1). Thus, reverseNum
        becomes 4321. Now, discarding the units's digit of num, we get num equal to 0, which indicates that there are no more
        digits to be processed and we have been able to build reverseNum as desired.

         Program 10.6 Write a function reverseDigits(num) to display reverse of a number.

          01 # Program Objective: To display reverse of a number
          02 def reverseDigits(num):
          03     """
          04     Objective: To find reverse of the number
          05     Inputs:
          06 num : number whose reverse has to be returned
          07     Return value:
          08          reverseNum : reverse of num
          09     """
          10     num1 = num
          11     reverseNum = 0
          12     while num > 0:
          13         remainder = num % 10
          14         reverseNum = reverseNum * 10 + remainder
          15         num = num // 10
          16     return reverseNum
          17 number = int(input("Enter a number : "))
          18 reverseNum = reverseDigits(number)
          19 print("The reverse of ", number, " is :", reverseNum)
        Sample Output:
         >>> Enter a number : 1234
              The reverse of 1234 is : 4321                                               Enter in loop
        As shown in the syntax for the while statement, it may include
        an optional else clause. It operates like the else clause in a for          Condition       False
        statement, i.e., the else clause gets executed on a smooth exit
        from the while loop. However, if control moves out of the while                  True
        loop on execution of the break statement, the else clause is
        ignored.
                                                                                                        else block

                                                                                   Encounter       Body
                                                                        no           break

                                                                                         yes
                                                                            Exit from loop

         246   Touchpad Computer Science-XI
   243   244   245   246   247   248   249   250   251   252   253