Python Programs
Q1. Write a program to enter the temperature in degree Celsius and convert it into Fahrenheit.
Answer:-
#defining the function to convert celcius into fahreheit
def fahren(t):
f=float((9*t)/5 + 32.0)
print(t,"degree celcius in fahrenheit = ",f," degree fahrenheit")
temp = int(input("enter the temperature in degree celcius:"))
fahren(temp) #calling the function
Output: enter the temperature in degree celcius:100
100 degree celcius in fahrenheit = 212.0 degree Fahrenheit
Q2. Write a program to enter two numbers and one operator(+, -, /, x) and print the corresponding sum, difference,
quotient or product.
Answer:- n=int(input("enter the first number: "))
m=int(input("enter the second number: "))
c=input("enter an operator: ")
if c=='+':
print("sum=",n+m)
elif c=='-':
if n>m:
print("difference=",n-m)
else:
print("difference=",m-n)
elif c=='*':
print("Product=",m*n)
elif c=='/':
print("Quotient=",n/m)
else:
print("Enter a valid operator")
Output:- enter the first number:34 enter the first number:34
enter the second number:23 enter the second number:23
enter an operator+ enter an operator-
sum= 57 difference= 11
------------------------------------------------------------------------------------------------------------
enter the first number:45 enter the first number:35
enter the second number:3 enter the second number:7
enter an operator* enter an operator/
Product= 135 Quotient= 5.0
Q3. Write a program to enter the units consumed and calculate the electricity bill as per following criteria:
Units Criteria
0 Fixed charge = Rs. 150
<=200 units Fixed charge + @Rs7/unit
<=300 units Fixed charge + @ Rs.7/unit for 1st 200 units + @Rs.8/unit for units above 200 units
>300 units Fixed charge+@ Rs.7/unit for 1st 200 units + @Rs.8/unit for next 100 units+@Rs10/unit for units above 300
Answer
unit=int(input("enter your units consumed = "))
:-
if unit == 0:
bill=150
elif unit <=200:
bill= 150 + 7*unit
elif unit<=300:
bill=150+7*200+8*(unit-200)
else:
bill=150+7*200+ 8*100 + 10*(unit-300)
print("Your Electricity bill = ",bill)
Output:- enter your units consumed = 345
Your Electricity bill = 2800
Q.4 Write a program to enter marks of five subjects and calculate the percent marks to determine the result as per
Following criteria:
percentage Result
<33% Failed
>=33% and <45% Third division
>=45% and <60% Second division
>=60% and <75% First division
>=75% and <90% Distinction marks
>=90% and<=100% Excellent
>100% Invalid marks
Answer:-
h=int(input("Enter Marks in Hindi = "))
e=int(input("Enter Marks in Englissh = "))
m=int(input("Enter Marks in Maths = "))
s=int(input("Enter Marks in Science = "))
sst=int(input("Enter Marks in S.St = "))
t=h+e+m+s+sst
per=t/5
print("Your total marks = ",t)
if per<33:
print("failed")
elif per<45:
print("third division")
elif per<60:
print("2nd division")
elif per<75:
print("1st division")
elif per<=90:
print("distinction marks")
elif per<=100:
print("excellent")
else:
print("invalid marks")
Output:- Enter Marks in Hindi = 56
Enter Marks in Englissh = 64
Enter Marks in Maths = 72
Enter Marks in Science = 59
Enter Marks in S.St = 94
Your total marks = 345
1st division
Q.5 Write a program to find the factorial of a number input by user, using a function.
Answer:- def fact(n):
f=1
for i in range(1,n+1):
f=f*i
return(f)
n=int(input("Enter a number for factorial : "))
f=fact(n)
print("Factorial of : ",n, " = ", f)
Output:- Enter a number for factorial : 5
Factorial of : 5 = 120
Q6. Write a program to print the specified number of terms for a Fibonacci series as:0, 1, 1, 2, 3, 5, 8, 13...........
Answer:- a=0
b=1
n=int(input("Enter number of terms fr Fibonacci series:"))
print(a,", ",end='')
print(b,", ",end='')
for i in range(1,n-1):
t=a
a=b
b=t+b
print(b,", ",end='')
Output:- Enter number of terms fr Fibonacci series:10
0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 ,
Q7. Write a program to print the following format:
5432123456
4321 3456
321 456
21 56
1 6
Answer:- for r in range(5,0,-1):
for c in range(r,0,-1):
print(c,end ='')
for sp in range(2*(r+1)-1,10,+1):
print(' ',end='')
for c in range(7-r,7,1):
print(c,end ='')
print('\n')
Output:- 5 4 3 2 1 2 3 4 5 6
4321 3456
321 456
21 56
1 6
Q.8 Write a program to enter a number and check if the number is prime or composite.
Answer:- n=int(input("Enter a number:"))
tag=0
for i in range (2,n):
if(n%i==0):
tag=1
if(tag==1):
print("Composite number")
else:
print("Prime Number")
Output:- Enter a number:45 Enter a number:59
Composite number Prime Number
Q.9 Write a program to enter a number and print the number in reverse order.
Answer: n=int(input("Enter a number :"))
m=n
rev=0
while(n>0):
rev=rev*10+n%10
n=int(n/10)
print("Reverse of given number ",m," = ",int(rev))
Output:- Enter a number :876757
Reverse of given number 876757 = 757678
Q10. Write a program to enter three numbers and find the largest of them.
Answer:- a=int(input("enter first number: "))
b=int(input("enter second number: "))
c=int(input("enter second number: "))
if (a>0) and (b>0) and (c>0):
if a>=b:
if a>=c:
print("Largest number = ", a)
else:
print("Largest number = ", c)
elif b>=c:
print("Largest number = ",b)
else:
print("Largest number = ",c)
else:
print("Please enter +ve integers only! Thank You!")
Output:- enter first number: 45
enter second number: 23
enter second number: 37
Largest number = 45
Q11. Write a program to enter a specified numbers of numbers in an empty list and print the list items in
Ascending/decsending order.
Answer:- numbers=[]
n=int(input("Please enter number of items for your number list:"))
if (n>0):
for i in range(0,n):
print("Enter item number ",i+1," of the number list: ",end='')
item=int(input())
numbers.append(item)
m=len(numbers)
numbers.sort()
print("The given numbers in ascending order are as follows: ")
m=len(numbers)
for i in range(0,m):
if (i!=m-1):
print(numbers[i],", ",end='')
else:
print(numbers[i])
else:
print("Please enter a positive number only:")
Order:- Please enter number of items for your number list:5
Enter item number 1 of the number list: 34
Enter item number 2 of the number list: 23
Enter item number 3 of the number list: 45
Enter item number 4 of the number list: 62
Enter item number 5 of the number list: 48
The given numbers in ascending order are as follows:
23 , 34 , 45 , 48 , 62
Q.12 Write a program to enter a Word/string and check if given string is a Palindrome ( the word which
is same as the original word when written in reverse order).
Answer:-
s=input("Enter a word of your choice : ")
n=len(s)
j=-1
flag=1
for i in range(0,int(n/2)+1):
if (s[i].lower()!=s[j].lower()):
flag=0
j=j-1
if (flag==1):
print("Given Word is a Palindromic word")
else:
print("given word is not a Palindromic word.")
Output:- Enter a word of your choice : Malayalam
Given Word is a Palindromic word
Q13. Write a program to enter the sides of a triangle and find its area using Heron’s formula.
print("Enter the first side of the triangle(in cm)= ",end='')
a=float(input())
print("Enter the second side of the triangle(in cm)= ",end='')
b=float(input())
print("Enter the third side of the triangle(in cm)= ",end='')
c=float(input())
s=(a+b+c)/2
area=pow(s*(s-a)*(s-b)*(s-c),0.5)
print("Area of the triangle = ",end='')
print(area," square cm")
Output:- Enter the first side of the triangle(in cm)= 20
Enter the second side of the triangle(in cm)= 15
Enter the third side of the triangle(in cm)= 25
Area of the triangle = 150.0 square cm
Q14. Write a program to enter the coefficient and constant term(a, b and c) of quadratic equation
ax2 + bx + c=0 and find the nature of the roots of the quadratic equation for given values of a, b
and c. Also find the value of roots, when the roots of the given quadratic equation are real.
Answers:- a=int(input("Enter the coeffcent of square of x in quadratic equation : "))
b=int(input("Enter the coeffcent of x in quadratic equation: "))
c=int(input("Enter the constant term in quadratic equation: "))
D=b*b-4*a*c
if D > 0:
print("Roots of given equation are real and distinct.")
fr=(-b+pow(D,0.5))/(2*a)
sr=(-b-pow(D,0.5))/2*a
print("First root = ",end='')
print(fr)
print("Second root = ",end='')
print(sr)
elif D==0:
print("Both the Roots of given equation are real and equal.")
fr=sr=-b/(2*a)
print("first root = ",end='')
print(fr)
print("second root = ",end='')
print(sr)
else:
print("Both the Roots of given equation are imaginary and distinct.")
Output :- Enter the coefficient of square of x in quadratic equation : 3
Enter the coefficient of x in quadratic equation: -10
Enter the constant term in quadratic equation: -8
Roots of given equation are real and distinct.
First root = 4.0
Second root = -6.0
Q15. Write a program to enter length, breadth, height of a cuboid and print it’s volume, lateral surface
area and total surface area.
print("Enter the length of the room(in cm) : ",end='')
l=float(input())
print("Enter the breadth of the room(in cm) : ",end='')
b=float(input())
print("Enter the height of the room(in cm) : ",end='')
h=float(input())
vol=l*b*h
lsa=2*(l+b)*h
tsa= 2*(l*b+b*h+h*l)
print("Volume of the given cuboid = ",end='')
print(vol, " cubic cm")
print("Lateral Surface Area of the given cuboid = ",end='')
print(lsa, " square cm")
print("Total Surface Area of the given cuboid = ",end='')
print(tsa," square cm")
Output:- Enter the length of the room(in cm) : 4
Enter the breadth of the room(in cm) : 5
Enter the height of the room(in cm) : 6
Volume of the given cuboid = 120.0 cubic cm
Lateral Surface Area of the given cuboid = 108.0 square cm
Total Surface Area of the given cuboid = 148.0 square cm