Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 127 - 2615_KEC
P. 127

    Program 32: To test whether a given number is prime number or not.

                         Program32.py
                      File  Edit  Format   Run    Options   Window    Help

                      # Program to check whether a number is prime or not
                      num = int(input("Enter a number: "))
                      if num > 1:
                          for i in range(2, num):
                              if num % i == 0:
                                  print("Number is not a prime.")
                                 break
                          else:
                              print("Number is a prime.")                                      Output
                      else:                                                                 Enter a number: 24
                          print("Number is not a prime.")                                   Number is not a prime.

                     Program 33: To calculate the factorial of a given number using a for loop.
                         Program33.py

                      File  Edit  Format   Run    Options   Window    Help

                      # Simple program to find factorial

                      num = int(input("Enter a number: "))

                      fact = 1
                      for i in range(1, num + 1):
                          fact *= i

                      print("Factorial is:", fact)

                         Output

                      Enter a number: 5
                      Factorial is: 120

                     Program 34: To create a library membership fee calculator. If the age of a person lies between 5
                     to 18 years, the membership fee will be Rs. 150. If the age of a person lies between 19 to 50 years,
                     the membership fee will be Rs. 400. If the age of a person is above 50 years, the membership fee
                     will be Rs. 250.

                                                                                            Control Structures in Python  125

| 122 123 124 125 126 127 128 129 130 131 132 |