0% found this document useful (0 votes)
8 views

Implementing programs using Functions Factorial of a given number

Notes about Implementing programs using Functions Factorial of a given number using Python

Uploaded by

mosesshaam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Implementing programs using Functions Factorial of a given number

Notes about Implementing programs using Functions Factorial of a given number using Python

Uploaded by

mosesshaam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Ex.

No:6a
Implementing programs using Functions
Factorial of a given number

Aim:
To write a python program to calculate the factorial value of a given number usingfunction.

Algorithm: Main function


Step 1: Start
Step 2: Get the value of n.
Step 3: Call the user-defined function ‘factorial()’by passing the value of ‘n’.
Step 4: Stop

Algorithm: factorial() function


Step 1: Get the value, n from caller function.
Step 2: Initialize the variables fact =1, i=1.
Step 3: Check the value of n. If n>= i then go to step 4 otherwise go to step 5.
Step 4: Calculate the following sequence of expressions in order
fact = fact *i
i= i+1
Go to step 4
Step 5: Display the value of factorial
Step 6: Return to main function.

Program:
print("Factorial of a given number")
def factorial(n):
fact=1
i=1
while(i<=n):
fact=fact*i
i=i+1
print("Factorial of “,n,” is”=",fact)
n=int(input("Enter the input value:"))
factorial(n)

Sample Output:
Enter a number:
The factorial of 6 is 720
Result:
Thus the program to calculate factorial of a given number was implemented and verified
successfully.
Ex.No: 6b
To find the largest number in a list

Aim:
To write a program to find the largest number in a list using python.

Algorithm: Main function


Step 1: Start
Step 2: Initialize an empty list lst_1
Step 3: Read number of elements to be store in a list lst_1
Step 4: In the for loop append each number to the list.
Step 5: Call the function find_max.
Step 6: Display the max in the list
Step 6: Stop
Algorithm: Function find_max()
Step 1: Assign max=list[0]
Step 2: For each item i in the list
Step 2.1: if i > max then max = i
Step 3: Return to main function
Program :
def find_max(list):
max = list[0]
for i in list:
if i> max:
max = i
return max
num = int(input('How many numbers: '))
lst_1 = []
for n in range(num):
numbers = int(input('Enter number '))
lst_1.append(numbers)
print("Maximum element in the list is :",find_max(lst_1))
Sample Output:
How many numbers: 5
Enter number 95
Enter number 63
Enter number 85
Enter number 74
Enter number 51
Maximum element in the list is: 95
Result:
Thus, the program to find the largest number in the list was implemented and verified successfully
Ex.No:6c
Find the area of different shapes

Aim:
To write a program to find the area of different shapes using python.
Algorithm:
Step 1: Start.
Step 2: Get the name of the shape
Step 3: Call the function calculate_area
Step 4: Stop
Algorithm: Calculate_area() function
Step 1: Convert name of the shape in to lower case.
Step 2: If name== “rectangle”
Step 2.1: Calculate area using the given length and breadth values and go to step7
Step 3: elif name== “square”
Step 3.1: Calculate area using the given length value and go to step 7
Step 4: elif name== “triangle”
Step 4.1: Calculate area using the given height and breadth values and go to step 7
Step 5: elif name== “circle”
Step 5.1: Calculate area using the given radius value and go to step 7
Step 6: otherwise display the shape is not available
Step 7: Return to main function.

Program:
def calculate_area(name):
name = name.lower()
if name == "rectangle":
l = int(input("Enter rectangle length in m: "))
b = int(input("Enter rectangle breadth in m: "))
rect_area = l * b
print("The area of rectangle is :",rect_area)
elif name == "square":
s = int(input("Enter square side lengthin m: "))
sqt_area = s * s
print(f"The area of square is: ",sqt_area)
elif name == "triangle":
h = int(input("Enter triangle height length in m: "))
b = int(input("Enter triangle breadth length in m: "))
tri_area = 0.5 * b * h
print(f"The area of triangle is:",tri_area)
elif name == "circle":
r = int(input("Enter circle radius lengthin m: "))
pi = 3.14
circ_area = pi * r * r
print(f"The area of circle is:",circ_area)
else:
print("Sorry! This shape is not available")

print("Calculate Shape Area")


shape_name = input("Enter the name of shape whose area you want to find: ")
calculate_area(shape_name)

Sample Output:
Calculate Shape Area
Enter the name of shape whose area you want to find: circle
Enter circle radius length in m: 5
The area of circle is: 78.5

Result:
Thus the program to find the area of different shapes was implemented and verified
successfully.
Ex. No: 7a
Implementing programs using strings
Reverse a string

Aim:
To write the python program to reverse the strings.

Algorithm:
Step 1: start
Step 2: Define the function rev_str with string argument
Step 2.1: Declare an empty string
Step 2.2: For each value in the argument repeat
Step 2.3: Concatenate the value with string
Step 2.4: Return the string
Step 3: Get the string
Step 4: Call rev_str function
Step 5: display the reverse string
Step 6: stop

Program:
def rev_str(s):
str = “”
for i in s:
str=i+str
return str
s=input(“Enter the string to reverse”)
print(“The original string is: ”,s)
print(“The reversed string is: “,rev_str(s))

Sample output:
Enter the string to reverse: PROGRAM
The original string is: PROGRAM
The reversed string is: MARGORP

Result:
Thus, the program to reverse the string was implemented and verified successfully.
Ex.No:7b
Palindrome of a String

Aim:
To write the python program for palindrome of a string

Algorithm:
Step 1: start
Step 2: Get the input string
Step 3: Check if the string is equal to the reversed string which is done using slicing operation.
Step 4: If condition is true display the string is palindrome otherwise display not palindrome.
Step 5: stop

Program:
string=input(("Enter a string:"))
if(string==string[::-l]):
print("The string is a palindrome")
else:
print("The string is not a palindrome")

Output:
Enter the string:madam
The string is palindrome

Result:
Thus, the program for palindrome of a string was implemented and verified successfully.
Ex.no:7.c
Character count

Aim:
To write the python program for character count

Algorithm:
Step 1: Start
Step 2: Get the string.
Step 3: Get the character to be counted in the string.
Step 4: For i is each character in string
Step 4.1: if i==character then
Count=count+1
Step 5: Display the character count
Step 6: Stop

Program:
str = input("Enter the String:")
character=input("Enter the Character:")
count =0
for i in str:
if i==character:
count=count+1
print(“The character Count in the given string is: “,count)

Sample output:
Enter the String: python programming
Enter the Character: p
The character count in the given string is: 2

Result:
Thus, the program for character count was implemented and verified
successfully.
Ex. No: 7d
Replacing characters

Aim:
To write a python program for character replacement

Algorithm:
Step 1: Start
Step 2: Read the String
Step 3: Replace the character in the string using replace() method.
Step 4: Display the replaced string.
Step 5: Stop

Program:
str = " Hi students, pWelcome to python programming students"
print ("Original String:",str)
str1=str.replace("Hi", "Hello")
print ("Replaced String 1:",str1)
str2=str1.replace("students", "STUDENTS", 2)
print ("Replaced String 2:",str2)

Sample output:
Original String: Hi students,Welcome to python programming students
Replaced String 1: Hello students,Welcome to python programming students
Replaced String 2: Hello STUDENTS,Welcome to python programming STUDENTS

Result:
Thus the program was implemented and executed successfully.

You might also like