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

num1, num2 = swap(num1,num2)

                      print('Numbers after swap',num1,num2)
                   16.  Write a program to repeat the string ‘‘GOOD MORNING” n times. Here ‘n’ is an integer entered by the user.
                  Ans.  def stringRepeat(myStr, num):
                          return myStr * num


                      myStr = 'GOOD MORNING'
                      num = int(input('Enter Number of times string to be repeated: '))

                      print('String repeated', num, 'times is:')
                      print(stringRepeat(myStr, num))
                   17.  Write a program to find average of three numbers.
                  Ans.  def average(num1,num2,num3):
                          avg = (num1 + num2 + num3) / 3
                          return avg

                      num1 = int(input("Enter First Number: "))
                      num2 = int(input("Enter Second Number: "))
                      num3 = int(input("Enter Third Number: "))
                      print("Average=",average(num1,num2,num3))
                   18.  The volume of a sphere with radius r is 4/3π. Write a Python program to find the volume of spheres with radius 7cm, 12cm, 16cm,
                      respectively.
                  Ans.  import math

                      def sphereVolume(radius):
                          volume = (4/3) * math.pi * radius**3
                          return volume


                      print(f"volume of sphere with radius 7cm: {sphereVolume(7):.2f}")
                      print(f"volume of sphere with radius 12cm: {sphereVolume(12):.2f}")
                      print(f"volume of sphere with radius 16cm: {sphereVolume(16):.2f}")
                   19.  Write a program that asks the user to enter their name and age. Print a message addressed to the user that tells the user the year in
                      which they will turn 100 years old.
                  Ans.   from datetime import datetime

                      def hundredYear(age):
                          year100 = int((100 - age) + datetime.now().year)
                          return year100

                      name = input('Enter Your Name:')
                      age = int(input('Enter your Age:'))

                      print ('Hello %s.You will turn 100 years old in %s.' % (name,hundredYear(age)))
                   20.  The formula E = mc2 states that the equivalent energy (E) can be calculated as the mass (m) multiplied by the speed of light (c = about
                      3×108 m/s) squared. Write a program that accepts the mass of an object and determines its energy.
                  Ans.   def computeEnergy(mass):
                          c = 3 * 10 ** 8
                          return (mass) * c ** 2

                                                                                             Data Types and Operators  187
   196   197   198   199   200   201   202   203   204   205   206