Python Main Programs Set 1
Python Main Programs Set 1
Aim:
To write a Python program that will convert the given temperature from Fahrenheit to Celsius and
vice versa based user choice.
Algorithm:
Step 1: Start the process.
Step 2: Prompt the user choice.
Step 3: If choice==1, then perform the following operations:
i) Read the value of f.
ii) Calculate the value of c = (5/9)*(f-32).
iii) Print the value of c.
Step 4: Else if choice==2, then perform the following operations:
i) Read the value of c.
ii) Calculate the value of f = (9/5)*c + 32.
iii) Print the value of f.
Step 5: Else, print warning the message “Prompt the choice from 1-2”.
Step 6: End the process.
Source Code:
#Python Program to convert the given temperature from Fahrenheit to Celsius and vice versa
print("1. Fahrenheit to Celsius Conversion")
print("2. Celsius to Fahrenheit Conversion")
choice=int(input("Enter Your Choice: "))
if choice==1:
f=int(input("Enter the temperature in Fahrenheit: "))
c=(5/9)*(f - 32)
print(f,"Degree Fahrenheit is equal to %.2f"%c,"Degree Celsius")
elif choice==2:
c=int(input("Enter the temperature in Celsius: "))
f = (9/5)*c + 32
print(c, "Degree Celsius is equal to %.2f"%f ,"Degree Fahrenheit")
else:
print("Prompt the choice from 1-2")
Output:
Result:
Thus the temperature conversion program using python has been executed successfully.
MAIN PROGRAM: 2
CALCULATING TOTAL MARKS, PERCENTAGE AND GRADE OF A STUDENT
Aim:
To write a Python program to read five subject marks and calculate the total marks, percentage
and grade of a student.
Algorithm:
Step 1 : Start.
Step 2 : Read the values of Roll_No, Stud_Name, M1, M2, M3, M4 and M5.
Step 3 : Calculate Total=M1+M2+M3+M4+M5.
Step 4 : Calculate Percentage=Total/5.
Step 5 : If Percentage>=80, then calculate Grade=”A”.
Step 6 : Else If Percentage >=70 and Percentage<80, then calculate Grade=”B”.
Step 7 : Else If Percentage >=60 and Percentage<70, then calculate Grade=”C”.
Step 8 : Else if Percentage >=40 and Percentage<60, then calculate Grade=”D”.
Step 9 : Else, calculate Grade="E”.
Step 10 : Print the values of Roll_No, Stud_Name, M1, M2, M3, M4, M5, Total, Percentage
and Grade.
Step 11 : Stop.
Source Code:
#Python Program to calculate the Total and Grade of a Student
Roll_No=int(input("Enter the Roll Number: "))
Stud_Name=input("Enter the Student Name : ")
M1=int(input("Enter the Mark1 : "))
M2=int(input("Enter the Mark2 : "))
M3=int(input("Enter the Mark3 : "))
M4=int(input("Enter the Mark4 : "))
M5=int(input("Enter the Mark5 : "))
Total=M1+M2+M3+M4+M5
Percentage=Total/5
if Percentage>=80:
Grade="A"
elif Percentage >=70 and Percentage<80:
Grade="B"
elif Percentage >=60 and Percentage<70:
Grade="C"
elif Percentage >=40 and Percentage<60:
Grade="D"
else:
Grade="E"
print("Roll Number :",Roll_No)
print("Student Name :",Stud_Name)
print("Mark1 :",M1)
print("Mark2 :",M2)
print("Mark3 :",M3)
print("Mark4 :",M4)
print("Mark5 :",M5)
print("Total :",Total)
print("Average :",Percentage)
print("Grade :",Grade)
Output:
Result:
Thus the Python program to calculate the total and grade of a student has been executed
successfully.
MAIN PROGRAM: 3
FINDING THE AREA OF A RECTANGLE, SQUARE, CIRCLE AND TRIANGLE
Aim:
To write a Python program to find the area of a Rectangle, Square, Circle and Triangle by
accepting suitable input parameters from user.
Algorithm:
Step 1: Start.
Step 2: Read the value of Shape.
Step 3: If Shape=1, then call the function Rectangle(), which will calculate the area of a
Rectangle.
Step 4: If Shape=2, then call the function Square(), which will calculate the area of a
Square.
Step 5: If Shape=3, then call the function Circle(), which will calculate the area of a
Circle.
Step 6: If Shape=4, then call the function Triangle(), which will calculate
the area of a Triangle.
Step 7: Else print the message “Select a valid shape”.
Step 8: Stop.
Source Code:
# Python Program to find to find the Area of a Rectangle, Square, Circle and Triangle
# Function to calculate the area of a Rectangle
def Rectangle():
l=float(input("Enter the Length of a Rectangle : "))
b=float(input("Enter the Breadth of a Rectangle : "))
R_Area = l * b
print("\nThe Area of a Rectangle is : %0.2f" %R_Area)
return
# Function to calculate the area of a Square
def Square():
s=int(input("Enter the side length of a Square : "))
S_Area=s*s
print("\nThe Area of a Square is : ",S_Area)
return
# Function to calculate the area of a Circle
def Circle():
PI=3.14
r=float(input("Enter the radius of a Circle: "))
C_Area = PI*r*r
print("\nThe Area of a Circle is : %0.2f"%C_Area)
return
# Function to calculate the area of a Triangle
def Triangle():
a=int(input("Enter the first side of a Triangle : "))
b=int(input("Enter the second side of a Triangle : "))
c=int(input("Enter the third side of a Triangle : "))
s=(a+b+c)/2
T_Area=(s*(s-a)*(s-b)*(s-c)) ** 0.5
print("The Area of a Triangle is : %0.2f"%T_Area)
return
# Main Program
print("1. Rectangle")
print("2. Square")
print("3. Circle")
print("4. Triangle")
Shape=int(input("Enter the shape you want to calculate the area: "))
if Shape==1:
Rectangle()
elif Shape==2:
Square()
elif Shape==3:
Circle()
elif Shape==4:
Triangle()
else:
print ("Select a Valid Shape")
Output:
Result:
Thus the Python program to find the area of a rectangle, square, circle and triangle has been
executed successfully.
MAIN PROGRAM: 4
PYTHON PROGRAM TO GENERATE FIBONACCI SERIES
Aim:
To write a Python program to display the first n terms of the Fibonacci Series.
Algorithm:
Step 1 : Start.
Step 2 : Read the value of n.
Step 3 :Assign f1=0, f2=1
Step 4 :Print the value of f1.
Step 5 :Print the value of f2.
Step 6 :Asssign i=3.
Step 7 :While (i<n):
i) Compute f3=f1+f2.
ii) Print the value of f3.
iii) Assign f1=f2.
iv) Assign f2=f3.
v) Increment I by 1.
Step 8 : Stop.
Source Code:
#Python Program to display the first n terms of the Fibonacci Series
n=int(input("How many terms? "))
f1=0
f2=1
print("The Fibonacci Series is")
print(f1)
print(f2)
i=3
while i<=n:
f3=f1+f2
print(f3)
f1=f2
f2=f3
i=i+1
Output:
Result:
Thus the Python program to generate the Fibonacci Series has been executed successfully.
MAIN PROGRAM: 5
FACTORIAL OF A GIVEN NUMBER USING RECURSION
Aim:
To write a Python program to find the factorial value of a given number using Recursion.
Algorithm:
Step 1 : Start.
Step 2 : Define a function named fact().
Step 3 : Read the value of n.
Step 4 : Call the function fact().
Step 5 : Print the factorial value.
Step 6 : Stop.
Source Code:
#Python Program to find the Factorial of a number using Recursion
def fact(m):
if m<=1:
return 1
else:
return m*fact(m-1)
# Main Program
n=int(input("Enter the value of n: "))
print("The factorial of",n,"is",fact(n))
Output:
Result:
Thus the Python program to find the factorial value of a given number has been executed
successfully.
MAIN PROGRAM: 6
COUNTING THE EVEN AND ODD NUMBERS FROM AN ARRAY OF N NUMBERS
Aim:
To write a Python program to count the Even and Odd numbers in an array of N numbers.
Algorithm:
Step 1 : Start.
Step 2 : Initialize the array a.
Step 3 : Initialize Event_Count=0, Odd_Count=0.
Step 4 : Read the value of N.
Step 5 : Iterate each element in the list using for loop and check if i % 2 == 0, the condition to
check even numbers.
Step 6 : If the condition satisfies, then increase Even_Count else increase Odd_Count.
Step 7 : Print the number of even numbers.
Step 8 : Print the number of odd numbers.
Step 9 : Stop.
Source Code:
#Python Program to Count the number of even and odd numbers from an array of N numbers
a=[]
Even_Count=0
Odd_Count=0
N=int(input("Enter the total number of elements in the array: "))
for i in range(1,N+1):
x=int(input("Enter the %d element: " %i))
a.append(x)
for i in a:
if i%2==0:
Even_Count=Even_Count+1
else:
Odd_Count=Odd_Count+1
print("Total Number of Even Numbers in the array = ", Even_Count)
print("Total Number of Odd Numbers in the array = ", Odd_Count)
Output:
Result:
Thus the Python program to count the Even and Odd numbers in an array has been executed
successfully.
MAIN PROGRAM: 7
COUNTING THE NUMBER OF UPPERCASE LETTERS AND LOWERCASE LETTERS IN A STRING
Aim:
To write a Python program to count the number of uppercase and lowercase letters in a string.
Algorithm:
Step 1 : Start.
Step 2 : Read the string Str.
Step 3 : Initialize the two count variables to 0.
Step 4 : Use a for loop to traverse through the characters in the string and increment the first count
variable each time an uppercase character is encountered and increment the second count
variable each time a lowercase character is encountered.
Step 5 : Print the total count of both the variables.
Step 6 : Stop.
Source Code:
#Python Program to count the number of Uppercase and Lowercase Letters in a String
Str=input("Enter the String: ")
Uppercase=0
Lowercase=0
for i in Str:
if (i.isupper()):
Uppercase+=1
elif (i.islower()):
Lowercase+=1
print("The number of uppercase characters is:")
print(Uppercase)
print("The number of lowercase characters is:")
print(Lowercase)
Output:
Result:
Thus the Python program to count the number of uppercase and lowercase letters in a string has
been executed successfully.
MAIN PROGRAM: 8
REVERSING A STRING AND CHECK FOR PALINDROME
Aim:
To write a Python program to reverse the given string and check for Palindrome.
Algorithm:
Step 1 : Start.
Step 2 : Read the string.
Step 3 : Find the reverse order of the string.
Step 4 : Print the string in reverse order.
Step 5 : If the original string and the reverse order are equal, then print the message “The given
string is Palindrome” else print the message “The given string is not Palindrome”.
Step 6 : Stop.
Source Code:
# Python Program to Reverse the given string and check for Palindrome
Str=input("Enter the String : ")
Rev_Str=""
for i in Str:
Rev_Str=i+Rev_Str
print("The String in reverse order : ", Rev_Str)
if(Str==Rev_Str):
print("The given string is Palindrome")
else:
print("The given string is not Palindrome")
Output:
Result:
Thus the Python program to reverse the given string and check for palindrome has been executed
successfully.