Page 303 - Computer Science V2.0 Class 11
P. 303

Sample Output:
                  >>> Enter the Strings:Madras
                      2
                 11.3.2 Factorial of a Number
                   Given a non-negative integer n, we wish to compute its factorial (n!).

                   We write a function to compute the factorial (n!) of a number n (>=0).
                   The function factorial accepts a positive integer as an argument and returns its factorial. We are aware that each of
                   0! and 1! equals 1.
                   To compute the factorial of a number (positive integer), we compute the product of positive integers up to n. For
                   example, to compute 6!, we compute the product 1*2*3*4*5*6. Thus, to compute 6!, we initialise the product to be
                   1, and for each number in the range(1,7), we multiply the product by it.
                   Note that the function range(1,7) returns a sequence of six integers: 1, 2, 3, 4, 5, 6.
                   In general, to compute n!, we compute the product 1*2*...*n of the numbers in the range(1,n+1).
                   Program 11.3 accepts a non-negative integer from the user and invokes the function factorial() to compute and
                   display its factorial.

                 Program 11.3 Compute and display n! for a number n, entered by the user.

                   01 def factorial(n):
                   02     '''
                   03     Objective: To compute factorial of a number
                   04     Input Parameter: n - numeric value
                   05     Return Value: factorial of n - numeric value
                   06     '''
                   07     product = 1
                   08     for i in range(1, n + 1):
                   09         product = product * i
                   10     return product
                   11 '''
                   12 User Interface:
                   13 Accept a number from the user interactively,
                   14 and display its factorial
                   15 '''
                   16 n = int(input('Enter a non-negative integer: '))
                   17 #ensure n is an integer and n>=0
                   18 assert n >= 0
                   19 product = factorial(n)
                   20 print('Factorial of', n, 'is', product)
                 Sample Output:
                  >>> Enter a non-negative integer: 6
                      Factorial of 6 is 720

                       Assert condition is the expression that is expected to be true. If the condition evaluates to False, an Assertion Error
                       is raised, and the program execution is halted.


                 11.3.3 Multiplication Table of a Number


                 Program 11.4 Write a function that accepts the number n whose multiplication table is to be printed as an argument
                 and prints its multiplication table.

                 To do so, we only need to multiply the given number by each number in the range(1, 11) and print the product.


                                                                                                   Looping in Python  289
   298   299   300   301   302   303   304   305   306   307   308