Implementing programs using Functions Factorial of a given number
Implementing programs using Functions Factorial of a given number
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.
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.
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")
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.