DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
WORKSHEET 1.3
Student Name: Daksh Dhamija UID: 21BCS5587
Branch: CSE Section/Group: 810 B
Semester: 4th Date of Performance: 23/02/23
Subject Name: Python Programming Subject Code: 21CSP 259
1. Aim: Write a program to find perimeter and area of circle using the radius.
Code:
import math
def circle_info(radius):
area = math.pi * radius**2
perimeter = 2 * math.pi * radius
return (area, perimeter)
radius = float(input(" the radius of the circle: "))
area, perimeter = circle_info(radius)
print("The area of the circle is:", round(area, 2))
print("The perimeter of the circle is:", round(perimeter, 2))
Daksh Dhamija 21BCS5587
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:
2. Aim: Write a program to print multiplication table from 2 to 20 as pe the input from the
user and return corresponding values.
Code:
def print_multiplication_table(number):
for i in range(2, 21):
print(number, "x", i, "=", number * i)
number = int(input("Enter a number: "))
print_multiplication_table(number)
Daksh Dhamija 21BCS5587
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:
3. Aim: Write a program to find factorial of a number using function.
Code:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
n = int(input("Enter a number: "))
result = factorial(n)
print("The factorial of", n, "is", result)
Daksh Dhamija 21BCS5587
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:
4. Aim: Write to return square and cube of a given number.
Code:
def square_and_cube(n):
square = n ** 2
cube = n ** 3
return (square, cube)
n = int(input("Enter a number: "))
square, cube = square_and_cube(n)
print("The square of", n, "is", square)
print("The cube of", n, "is", cube)
Output:
Daksh Dhamija 21BCS5587
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
5. Aim: Program to find the roots of a quadratic equation.
Code:
import math
def find_roots(a, b, c):
discriminant = b**2 - 4*a*c
if discriminant < 0:
print("No real roots")
elif discriminant == 0:
root = -b / (2*a)
print("One real root:", root)
else:
root1 = (-b + math.sqrt(discriminant)) / (2*a)
root2 = (-b - math.sqrt(discriminant)) / (2*a)
print("Two real roots:", root1, "and", root2)
a = float(input("Enter the value of coefficient a: "))
b = float(input("Enter the value of coefficient b: "))
c = float(input("Enter the value of coefficient c: "))
find_roots(a, b, c)
Output:
Daksh Dhamija 21BCS5587
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
6. Aim: If you are given 3 sticks, you may or may not be able to arrange that in a triangle.
For any three, there is a simple test to see if it is possible to from a triangle.
If any of the length is greater than the some of the other two the triangle can’t be form.
Code:
def is_triangle(a, b, c):
if a <= 0 or b <= 0 or c <= 0:
return False
if a + b <= c or b + c <= a or a + c <= b:
return False
return True
a = float(input("Enter the length of side a: "))
b = float(input("Enter the length of side b: "))
c = float(input("Enter the length of side c: "))
if is_triangle(a, b, c):
print("A triangle can be formed with sides of length", a, ",", b, "and", c)
else:
print("A triangle cannot be formed with sides of length", a, ",", b, "and", c)
Output:
Daksh Dhamija 21BCS5587
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING