#4.1 gratuity calculator bill_amount = float(input("Enter the bill amount: ")) gratuity_rate = float(input("Enter the gratuity rate as a percentage: ")) gratuity_amount = bill_amount * (gratuity_rate / 100) total_amount = bill_amount + gratuity_amount print("Gratuity amount: ", format(gratuity_amount, ".2f")) print("Total bill amount: ", format(total_amount, ".2f")) Enter the bill amount: 1000 Enter the gratuity rate as a percentage (e.g. 15): 15 Gratuity amount: $ 150.00 Total bill amount: $ 1150.00 #4.2 Find the two highest scores arr = [] n = int(input("Enter the number of scores: ")) for x in range(n): p = int (input("Enter the value for score %d: "%(x+1))) arr.append(p) arr.sort() print("Highest score is {} \nSecond Highest score is {}".format(arr[-1],arr[-2])) Enter the number of scores: 5 Enter the value for score 1: 45 Enter the value for score 2: 34 Enter the value for score 3: 67 Enter the value for score 4: 89 Enter the value for score 5: 78 Highest score is 89 Second Highest score is 78 #4.3 decimal to binary def convertToBinary(n): if n > 1: convertToBinary(n//2) print(n % 2,end = '') dec = int(input()) convertToBinary(dec) print() 36 100100 # 4.4 find the LCM of two numbers def lcm(a, b): if a > b: larger = a smaller = b else: larger = b smaller = a multiple = larger while multiple % smaller != 0: multiple += larger return multiple print(lcm(20, 15)) 60 # 4.5 Find whether the given number is Armstrong number num = int(input("Enter a number: ")) num_str = str(num) n = len(num_str) sum = 0 for digit in num_str: digit_int = int(digit) sum += digit_int ** n if sum == num: print(num, "is an Armstrong number") else: print(num, "is not an Armstrong number") Enter a number: 333 333 is not an Armstrong number #4.6 Find the area of the triangle base = float(input("Enter the base of the triangle: ")) height = float(input("Enter the height of the triangle: ")) area = (base * height) / 2 print("The area of the triangle is:", area) Enter the base of the triangle: 4 Enter the height of the triangle: 8 The area of the triangle is: 16.0 # 4.7 simple interest principal = 1000 rate = 0.05 time = 2 interest = principal * rate * time print("Simple Interest: ", interest) Simple Interest: 100.0 # 4.8 Solve quadratic equation: ax 2 + bx + c = 0 import math a = float(input("Enter coefficient of x^2: ")) b = float(input("Enter coefficient of x: ")) c = float(input("Enter constant term: ")) discriminant = b**2 - 4*a*c if discriminant >= 0: root1 = (-b + math.sqrt(discriminant)) / (2*a) root2 = (-b - math.sqrt(discriminant)) / (2*a) print("The real roots are:", root1, "and", root2) else: real_part = -b / (2*a) imaginary_part = math.sqrt(abs(discriminant)) / (2*a) print("The complex roots are:", real_part, "+", imaginary_part, "i and", real_part, "-", imaginary_part, "i") Enter coefficient of x^2: 1 Enter coefficient of x: 1 Enter constant term: 1 The complex roots are: -0.5 + 0.8660254037844386 i and -0.5 - 0.8660254037844386 i exp 5 !pip install ColabTurtlePlus==1.5 # 5.1 triangle import ColabTurtlePlus.Turtle as t t.initializeTurtle() t.forward(100) t.left(120) t.forward(100) t.left(120) t.forward(100) # 5.1. square import ColabTurtlePlus.Turtle as bound bound.initializeTurtle() bound.fd(100) bound.rt(90) bound.fd(100) bound.rt(90) bound.fd(100) bound.rt(90) bound.fd(100) # 5.1. pentagon import ColabTurtlePlus.Turtle as pen pen.initializeTurtle() pen.fd(100) pen.lt(72) pen.fd(100) pen.lt(72) pen.fd(100) pen.lt(72) pen.fd(100) pen.lt(72) pen.fd(100) #5.1.hexagon import ColabTurtlePlus.Turtle as hex hex.initializeTurtle() num_sides=6 length=100 angle=360/num_sides for i in range(num_sides): hex.fd(length) hex.rt(angle) # 5.2 draw circle import ColabTurtlePlus.Turtle as cir cir.initializeTurtle() cir.circle(100) # 5.3 printing a alphabet import ColabTurtlePlus.Turtle as a a.initializeTurtle() a.penup() a.goto(-30,50) a.pendown() a.pensize(10) a.right(65) a.forward(100) a.setpos(-30,50) a.right(50) a.forward(100) a.penup() a.setpos(-50,-10) a.right(65) a.pendown() a.backward(50) #5.3.alphabet k import ColabTurtlePlus.Turtle as k k.initializeTurtle() k.penup() k.goto(-30,50) k.pendown() k.pensize(10) k.right(90) k.forward(150) k.goto(-30,-20) k.left(45) k.forward(100) k.goto(-30,-20) k.left(90) k.forward(100) #5.4 flower import ColabTurtlePlus.Turtle as t t.initializeTurtle() t.speed(7) t.penup() t.goto(0, -200) t.pendown() t.pensize(10) t.pencolor("green") t.setheading(90) t.forward(90) t.pensize(3) petal_colors = ["red", "orange", "yellow", "green", "blue", "purple"] for i in range(6): t.fillcolor(petal_colors[i]) t.begin_fill() t.setheading(i * 60) t.circle(50, 60) t.left(120) t.circle(50, 60) t.end_fill() t.penup() t.goto(0,-110) t.pendown() t.pencolor("brown") t.fillcolor("yellow") t.begin_fill() t.circle(1) t.end_fill() #5.5 spiral square inside out import ColabTurtlePlus.Turtle as spsq spsq.initializeTurtle() spsq.speed(10) size = 10 for i in range(20): spsq.forward(size) spsq.right(90) spsq.forward(size) spsq.right(90) size += 10 #5.6 spiral hexagon import ColabTurtlePlus.Turtle as sphe sphe.initializeTurtle() sphe.speed(10) size = 20 for i in range(15): for j in range(6): t.forward(size) t.right(60) size += 10