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

return "Ms. "+ name
                          else:
                              return "Invalid gender specified."

                      # Example usage
                      name = input("Enter the name: ")
                      gender = input("Enter the gender (M/F): ")

                      updatedName = addPrefix(name, gender)
                      print(updatedName)
                    3.  Write a program that has a user defined function to accept the coefficients of a quadratic equation in variables and calculates its
                      determinant. For example: if the coefficients are stored in the variables a,b,c then calculate determinant as b2-4ac. Write the appropriate
                      condition to check determinants on positive, zero and negative and output appropriate result.
                  Ans.
                      def calculateDeterminant(a, b, c):

                          determinant = b**2 - 4*a*c
                          return determinant

                      print("Enter the coefficients for quadratic equation")
                      a = float(input("a: "))
                      b = float(input("b: "))
                      c = float(input("c: "))

                      # Calculate determinant
                      determinant = calculateDeterminant(a, b, c)

                      if determinant > 0:
                          print("Positive Determinant")
                          print("The quadratic equation has two distinct real roots.")
                      elif determinant == 0:
                          print("Zero determinant")
                          print("The quadratic equation has one real root (a repeated root).")
                      else:
                          print("Negative Determinant")
                          print("The quadratic equation has two complex conjugate roots.")
                    4.  ABC School has allotted unique token IDs from (1 to 600) to all the parents for facilitating a lucky draw on the day of their Annual day
                      function. The winner would receive a special prize. Write a program using Python that helps to automate the task. (Hint: use random
                      module)
                  Ans.

                      import random
                      # Number of participants with unique token IDs (1 to 600)
                      numParents = 600

                      def luckyDraw(numParents):
                          winId = random.randint(1, numParents)
                          return winId


                      # Perform the lucky draw
                      winner = luckyDraw(numParents)
                      print("Winner of the lucky draw!", winner)

                                                                                             Introduction to Functions  225
   234   235   236   237   238   239   240   241   242   243   244