Page 216 - Computer Science Class 11 Without Functions
P. 216

Program 9.6 Write a program to find reverse of the number.

          01 '''
          02 Objective: To find reverse of the number
          03 Input:
          04        num : number whose reverse has to be returned
          05 Output: reverse of num
          06 '''
          07 num = int(input('Enter a number: '))
          08 reverseNum = 0
          09 while num > 0:
          10     remainder = num % 10
          11     reverseNum = reverseNum * 10 + remainder
          12     num = num // 10
          13 print('Reverse: ', reverseNum)
        Sample Output:
         >>> Enter a number: 1234
              Reverse: 4321
        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 statement, i.e., the else clause gets executed on a smooth exit from the while loop. However, if control
        moves out of the while loop on execution of the break statement, the else clause is ignored.

                                                         Enter in loop

                                                                     False
                                                   Condition


                                                         True



                                                                         else block

                                                                    Body
                                                   Encounter
                                     no             break


                                                       yes
                                            Exit from loop
                                                          Fig 9.4

        9.4.3 for Statement Vs while Statement

        We have already learnt that each of the for and while statements is used for looping. We have already used a for
        statement to develop a program that finds the factorial of a number.  Now we ask the question: can we compute the
        factorial of a number using a while  statement? Of course, we can, as illustrated in Program 9.7. A more serious
        question to ask is whether we should write a program to compute the factorial of a number using a while statement.
        The answer to this question is a resounding "NO." When all we need to do is count up or down, a certain number of
        times, a for statement should be the natural choice because that is how we think. For example, while moderating the
        results of an examination, a teacher might tell an official: Increase each student's marks by two. In contrast, imagine
        the teacher telling the official: while there are students in the class, increase a student's marks by two. Repeat the
        process of checking and updating the student's marks until there are no more students left. How incomprehensible
        is the second version? The same thing applies to computer programs. As the programs developed by a programmer
        need to be maintained. To make the programs readable, we need to be judicious in our choice of the control structures
        (or in fact, any syntactic structure), as indiscriminate use of the language's syntax makes the programs unreadable.





         214   Touchpad Computer Science-XI
   211   212   213   214   215   216   217   218   219   220   221