Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 84 - 2611_SmartGPT Pro V(5.0) C-8
P. 84

    Program 5: To print multiplication table of number taken as input from user.

                          Program5.py

                       File  Edit  Format   Run   Options  Window    Help

                       #program to calculate multiplication table of a number
                       num= int(input('Enter a number: '))
                       for i in range (1, 11):
                           print (num, 'x',i,'=', num * i)

                      On running the above program, you will get the following output:

                          Output
                       Enter a number: 6
                       6 x 1 = 6
                       6 x 2 = 12
                       6 x 3 = 18
                       6 x 4 = 24
                       6 x 5 = 30
                       6 x 6 = 36
                       6 x 7 = 42
                       6 x 8 = 48
                       6 x 9 = 54
                       6 x 10 = 60

                                THE WHILE LOOP

                      The while loop executes a set of statements repeatedly, until the logical expression evaluates to

                      false. When the condition becomes false, the control comes out of the loop.
                      The syntax of while statement is given below:

                      while (test expression):
                             Statements
                             increment/decrement expression
                      Program 6: To print first 5 natural numbers using while loop.

                          Program6.py                                               Output
                       File  Edit  Format   Run   Options  Window    Help         1
                                                                                  2
                       i=1
                                                                                  3
                       while i<6:
                                                                                  4
                           print(i)
                                                                                  5
                           i+=1

                       82   Computer Science (V5.0)-VIII

| 79 80 81 82 83 84 85 86 87 88 89 |