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

python-2

Uploaded by

kr861618
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

python-2

Uploaded by

kr861618
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Convert the Given Temperature from Fahrenheit to Celsius and

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 2: Read the input values using input function

Step 3:

a) if num==1 then

1. Read the input value Fahrenheit using input function

2. Use the formula Celsius = (Fahrenheit -32) * 5/9

3. Print value of Fahrenheit, Celsius.

Step 4: b) else

1. Read the input value Celsius using input function

2. Set Compute the Fahrenheit = (Celsius*9/5) +32

3. Print value of Celsius, Fahrenheit.

Step 5: Stop
Code:
print("Press 1 to Fahrenheit to Celsius")

print("Press 2 to Celsius to Fahrenheit")

num =(input("Enter your Choice:"))

if num == "1":

fahrenheit = float(input("Enter temperature in fahrenheit: "))

celsius = (fahrenheit - 32) * 5/9

print('%.2f Fahrenheit is: %0.2f Celsius' %(fahrenheit, celsius))

else:

celsius = float(input("Enter temperature in celsius: "))

fahrenheit = (celsius * 9/5) + 32

print('%.2f Celsius is: %0.2f Fahrenheit ' %(celsius, fahrenheit))


Output:

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 3: Set compute the value avg = (sub1+sub2+ sub3+sub4+sub5)/5.

Step 4: If the average is greater than 80, “Grade: A” is printed.

Step 5: If the average is in between 80 and 70, “Grade: B” is printed.

Step 6: If the average is in between 70 and 60, “Grade: C” is printed.

Step 7: If the average is in between 60 and 40, “Grade: D” is printed.

Step 8: If the average is anything below 60, “Grade: F” is printed.

Step 9: Stop
Code:

sub1=int(input("Enter marks of the first subject: "))

sub2=int(input("Enter marks of the second subject: "))

sub3=int(input("Enter marks of the third subject: "))

sub4=int(input("Enter marks of the fourth subject: "))

sub5=int(input("Enter marks of the fifth subject: "))

avg=(sub1+sub2+sub3+sub4+sub4)/5

if(avg>=80):

print("Grade: A")

elif(avg>=70 and avg<80):

print("Grade: B")

elif(avg>=60 and avg<70):

print("Grade: C")

elif(avg>=40 and avg<60):

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:

Write a Program to display the first n terms of Fibonacci series..

Algorithm:

Step 1: Start

Step 2: Create a function using def.

Step 3: Input the number of terms “terms”

Step 4: Assign the values f1=0, f2=1

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

for x in range(0, n):

print(f2, end = " ")

next = f1 + f2

f1 = f2

f2 = next

# Driven code

num=int(input("Enter The number:"))

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

Step 2: Create a function using def.

Step 3: if input value is equal to 1

1. Return n value 1

Step 4 : if input value is not equal to 1

1. Define recursive expression for computational processing

2.. Compute and return value n*recur_fact(n-1)

Step 5 : Repeat the steps until i=n

Step 6 : if input value is equal to 0

1. Return n value 0

Step 7: print the result of program.

Step 8: Stop
Code:

# Factorial of a number using recursion

def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = int(input("Enter The Number :"))

# check if the number is negative


if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))
Output:

Result:

Thus the Program to find factorial of the given number using recursive function is
executed and the output is obtained.

You might also like