python-2
python-2
Ex No: 1
Vice Versa Depending upon User’s Choice.
Date:
Aim:
To convert the given temperature from Fahrenheit to Celsius and vice versa depending
upon user’s choice.
Algorithm:
Step 1: Start
Step 3:
a) if num==1 then
Step 4: b) else
Step 5: Stop
Code:
print("Press 1 to Fahrenheit to Celsius")
if num == "1":
else:
Result:
Thus the program to convert the given temperature from Fahrenheit to Celsius is executed
and the output is obtained.
Calculate total marks, percentage and grade of a student.
Ex No: 2
Date:
Aim:
To calculate total marks, percentage and grade of a student. Marks obtained in each of the
five subjects are to be input by user
Algorithm:
Step 1: Start
Step 2: Read the five subject input values one by one using input function.
Step 9: Stop
Code:
avg=(sub1+sub2+sub3+sub4+sub4)/5
if(avg>=80):
print("Grade: A")
print("Grade: B")
print("Grade: C")
print("Grade: D")
else:
print("Grade: F")
Output:
Result:
Thus the Program to calculate total marks, percentage and grade of a student is executed
and the output is obtained.
Ex No: 3 Area of rectangle, square, circle and triangle by accepting suitable input parameters
from user.
Date:
Aim:
To find the area of rectangle, square, circle and triangle by accepting suitable input
parameters from user.
Algorithm:
Step 1: Start
Step 2: Create a function using def.
Step 3: Read one input calculate value using input function.
Step 4: Assign the values of area=0, Pie = 3.14.
Step 5: if input value is equal to square
. 1. Read input side value using input function
2. CALCULATE area = area+(side**2)
Step 6: if input value is equal to Circle
. 1. Read input radius value using input function
2. CALCULATE area = area+(2*pie*radius)
Step 7: if input value is equal to Rectangle
. 1. Read two input values length and width using input function
2. CALCULATE area = area+(length * width)
Step 8: if input value is equal to Triangle
. 1. Read two input values base and height using input function
2. CALCULATE area = area+(0.5*base*height)
Step 9: if input value is not a valid shape.
Step 10: print the result of program.
Step 11: Stop
Code:
#area calculator
def areacalculator():
_input_ = input("Enter the shape you want to calculate area of: ")
area = 0
pie = 3.14
if _input_ == "Square":
side = int(input("Enter the value of side: "))
area = area + (side ** 2)
elif _input_ == "Circle":
radius = int(input("Enter the value of radius: "))
area = area + (2 * pie * radius)
elif _input_ == "Rectangle":
length = int(input("Enter the value of length: "))
width = int(input("Enter the value of Width: "))
area = area + (length * width)
elif _input_ == "Triangle":
base = int(input("Enter the value of base: "))
height = int(input("Enter the value of height: "))
area = area +(0.5 * base * height)
else:
print ("Select a valid shape")
print ("%.2f" % area)
areacalculator()
Output:
Result:
Thus the Program to find the area of rectangle, square, circle and triangle is executed and
the output is obtained.
Ex No: 4 Display the first n terms of Fibonacci series.
Date:
Aim:
Algorithm:
Step 1: Start
Step 5: Then print the value of f1.add f1 and f2 and save it to nth variable.
Step 6: assign f1=f2 and f2=next and increment the count value
Step 7: Finally print the value of f1 until the loop condition satisfied.
Step 8: Stop
Code:
def printFibonacciNumbers(n):
f1 = 0
f2 = 1
if (n < 1):
return
next = f1 + f2
f1 = f2
f2 = next
# Driven code
printFibonacciNumbers(num)
Output:
Result:
Thus the Program to display the first n terms of Fibonacci series is executed and the output
is obtained.
Find Factorial of the Given Number using Recursive Function.
Ex No: 5
Date:
Aim:
Write a Program to find factorial of the given number using recursive function
Algorithm:
Step 1: Start
1. Return n value 1
1. Return n value 0
Step 8: Stop
Code:
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = int(input("Enter The Number :"))
Result:
Thus the Program to find factorial of the given number using recursive function is
executed and the output is obtained.