Page 240 - Computer Science V2.0 Class 11
P. 240
5. Write a program that implements a user defined function that accepts Principal Amount, Rate, Time, Number of Times the interest is
compounded to calculate and displays compound interest.
nt
(Hint: CI=P*(1+r/n) )
Ans.
def interest(principal, rate, time, numTimes):
#Approach: Formula:CI = principal(1 + rate/numTimes)^(numTimes*time)
compoundInterest = principal*(1 + rate/numTimes)**(numTimes*time)
return compoundInterest
# Prompt the user to enter details
principal = float(input("Enter the Principal Amount: "))
rate = float(input("Enter the Annual Interest Rate (in percentage): "))
time = float(input("Enter the Time in Years: "))
frequency = int(input("Enter the Number of Times Interest"+\
"is Compounded per Year: "))
# Calculate compound interest
compoundInterest = interest(principal, rate, time, frequency)
print("Compound Interest:",compoundInterest)
6. Write a program that has a user defined function to accept 2 numbers as parameters, if number 1 is less than number 2 then numbers
are swapped and returned, i.e., number 2 is returned in place of number1 and number 1 is reformed in place of number 2, otherwise the
same order is returned.
Ans.
def swapIfLess(num1, num2):
if num1 < num2:
return num2, num1
else:
return num1, num2
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
newNum1, newNum2 = swapIfLess(num1, num2)
# Display the result
print("Original Numbers:", num1, num2)
print("Updated Numbers:", newNum1, newNum2)
7. Write a program that contains user defined functions to calculate area, perimeter or surface area whichever is applicable for various
shapes like square, rectangle, triangle, circle and cylinder. The user defined functions should accept the values for calculation as
parameters and the calculated value should be returned. Import the module and use the appropriate functions.
Ans.
import math
# Function to calculate the area of a square
def computeSquareArea(side):
return side**2
# Function to calculate the perimeter of a square
def computeSquarePerimeter(side):
return 4 * side
226 Touchpad Computer Science (Ver. 2.0)-XI

