Ex. No.
3A
FACTORIAL OF A NUMBER USING FUNCTION
Date:
Aim:
To write a python program to find the factorial of a number using functions.
Algorithm:
Step 1: Start
Step 2: Read a number n
Step 3: If n=1 print 1 as factorial.
Step 4: Otherwise calculate n*fact(n-1) by calling recursive function and print fact.
Step 5: Stop.
Program:
# Factorial using recursion
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
num = int(input("Enter a number: "))
print("Factorial of", num, "is", factorial(num))
OUTPUT:
Result:
Thus, the python program to calculate factorial is executed and the output was
verified.
Ex. No. 3B
FINDING LARGEST NUMBER IN A LIST USING FUNCTION
Date:
Aim:
To write a python program to find the largest number in a list using functions.
Algorithm:
Step 1: Start
Step 2: Create a list and read the numbers
Step 3: Append them in a list.
Step 4: Use max built in function to find the largest element in a list.
Step 5: Print the largest number
Step 6; Stop.
Program:
Def myMax(list1):
Print(“Largest element is: “, max(list1))
List1=[]
Num=int(input(“Enter number of elements in list:”))
for I in range(1, num+1)
ele=int(input(“Enter elements:”))
[Link](ele)
print(“Largest elements is:”,myMax(list1))
Output:
Result:
Thus, a python program to find the largest number in a list is executed and the output
was verified.
Ex. No.: 3C
FINDING AREA OF A CIRCLE USING FUNCTION
Date:
Aim:
To write a Python program using a function to calculate the area of a circle.
Algorithm:
Step 1: Start the program.
Step 2: Define a function area_of_circle(radius) that:
Calculates the area using the formula
Area=π×r2\text{Area} = \pi \times r^2Area=π×r2
Returns the area.
Step 3: In the main program:
Input the radius.
Call the function and store the result.
Display the area.
Step 4: Stop.
Program:
# Program to find the area of a circle using function
def area_of_circle(radius):
pi = 3.14159
return pi * radius * radius
# Main program
r = float(input("Enter the radius of the circle: "))
area = area_of_circle(r)
print("Area of the circle with radius", r, "is", area)
Output:
Result:
The program successfully calculates the area of a circle for the given radius.