Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

MCA 2018 - 2020 PYTHON LAB

PROGRAM 1
Program to find the largest and smallest integer among three declared integers

SOURCE CODE

a=int(input("Enter three numbers\n"))


b=int(input())
c=int(input())
if (a>b and b>c):
print("the largest number is:",a)
elif ( b>c):
print("the largest number is:",b)
else:
print("the largest number is:",c)
if(a<b and b<c):
print("the smallest number is:",a)
elif(b<c):
print("the smallest number is:",b)
else:
print("the smallest number is:",c)

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 1


MCA 2018 - 2020 PYTHON LAB

PROGRAM 2
Program to check whether given year is a leap year or not

SOURCE CODE

n=int(input("enter one year\n"))


if(n%4==0 and n%100!=0 or n%400==0):
print("the year is leap year")
else:
print("the year is not a leap year")

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 2


MCA 2018 - 2020 PYTHON LAB

PROGRAM 3
Program to find the factorial of a given number

SOURCE CODE

n=int(input("enter a number\n"))
fact=1
for i in range(1,n+1):
fact=fact*i
print("the factorial is :",fact)

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 3


MCA 2018 - 2020 PYTHON LAB

PROGRAM 4
Program to check whether given number is Palindrome or not

SOURCE CODE

n=int(input("enter a number\n"))
rev=0
temp=n
while(n>0):
r=n%10
rev=rev*10+r
n=n//10
if(temp==rev):
print("the number is palindrome")
else:
print("the number is not a palindrome")

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 4


MCA 2018 - 2020 PYTHON LAB

PROGRAM 5
Program to check whether given number is Armstrong or not

SOURCE CODE

n=int(input("enter a number\n"))
arm=0
temp=n
while(n>0):
r=n%10
arm=arm+r**3
n=n//10
if(temp==arm):
print("the number is armstrong")
else:
print("the number is not armstrong")

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 5


MCA 2018 - 2020 PYTHON LAB

PROGRAM 6
Program to calculate simple interest

SOURCE CODE

p=float(input("enter the principle amount\n"))


n=int(input("enter the number of years\n"))
r=float(input("enter the rate of intrest\n"))
s=(p*n*r)/100
print("the simple intrest is:",s)

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 6


MCA 2018 - 2020 PYTHON LAB

PROGRAM 7
Program to print the Fibonacci series up to a given limit

SOURCE CODE

n=int(input("ënter the limit\t"))


f1=1
f2=1
print("The fibonacci series is ")
print(f1,end=" ")
print(f2,end=" ")
for i in range(n-2):
f3=f1+f2
print(f3,end=" ")
f1=f2
f2=f3

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 7


MCA 2018 - 2020 PYTHON LAB

PROGRAM 8
Program to convert Decimal number to Binary number

SOURCE CODE

def dectobin(dec):
if dec>=1:
dectobin(dec//2)
print(dec%2,end="")
dec=int(input("enter a decimal number\t"))
dectobin(dec)

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 8


MCA 2018 - 2020 PYTHON LAB

PROGRAM 9
Program to find the reverse of a given number

SOURCE CODE

n=int(input("enter a number\t"))
rev=0
while(n>=1):
r=n%10
rev=rev*10+r
n=n//10
print("the reverse of the number is:",rev)

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 9


MCA 2018 - 2020 PYTHON LAB

PROGRAM 10
Program to print all numbers in a range divisible by a number

SOURCE CODE

lower=int(input("enter the lower limit\t"))


upper=int(input("enter the upper limit\t"))
n=int(input("enter the number to by which you divide\t"))
print("The numbers divisible by ",n," in the given range are")
for i in range(lower,upper+1):
if(i%n==0):
print(i,end=" ")

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 10


MCA 2018 - 2020 PYTHON LAB

PROGRAM 11
Program to find the reverse of a string using recursion

SOURCE CODE

def reverse(strg):
if len(strg) == 0:
return strg
else:
return reverse(strg[1:]) + strg[0]
a = input("Enter the string to be reversed: ")
print(reverse(a))

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 11


MCA 2018 - 2020 PYTHON LAB

PROGRAM 12
Program to form a new string using the first two and last two characters of a given
string

SOURCE CODE

s=input("enter the string\n")


new=s[0:2]+s[len(s)-2:]
print("The new string is ")
print(new)

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 12


MCA 2018 - 2020 PYTHON LAB

PROGRAM 13
Program to generate a dictionary containing n numbers as keys and its squares as
values

SOURCE CODE

n=int(input("Enter a number:"))
d={x:x*x for x in range(1,n+1)}
print("the resultant dictionary is ")
print(d)

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 13


MCA 2018 - 2020 PYTHON LAB

PROGRAM 14
Program to find the number of digits in a number

SOURCE CODE

num=int(input("enter the number\n"))


cnt=0
while num>0:
d=num%10
cnt=cnt+1
num=num//10
print("the number of digits is ",cnt)

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 14


MCA 2018 - 2020 PYTHON LAB

PROGRAM 15
Program to find the area of a rectangle using classes

SOURCE CODE

class Rectangle:
def __init__(self,l,b):
self.l=l
self.b=b
def area(self):
print("Area of the rectangle is",self.l*self.b)
#main program
len=int(input("enter the length\n"))
brd=int(input("enter the breadth\n"))
rect=Rectangle(len,brd)
rect.area()

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 15


MCA 2018 - 2020 PYTHON LAB

PROGRAM 16
Program to implement list operations (Append, Delete and Display) using Classes.

SOURCE CODE

class Listop:
def append(self,elm,l):
l.append(elm)
def delete(self,pos,l):
del l[pos]
def display(self,l):
print(l)
#main program
s=input("enter the list elements\t")
l=s.split()
print("==============LIST OPERATIONS==========")
print("1.Append")
print("2.Delete")
print("3.Display")
print("4.Exit")
print("=======================================")
while True:
c=int(input("enter the choice\t"))
ob=Listop()
if c==4:
break
if c==1:
n=int(input("enter the element to append\t"))
ob.append(n,l)
elif c==2:
p=int(input("enter the position to delete\t"))
ob.delete(p,l)
elif c==3:
ob.display(l)
print("=======================================")

PG Dept of Computer Applications, Marian College Kuttikanam Page 16


MCA 2018 - 2020 PYTHON LAB

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 17


MCA 2018 - 2020 PYTHON LAB

PROGRAM 17
Program to create a class and compute the Area and Perimeter of a Circle

SOURCE CODE

import math
class Circle:
def __init__(self,r):
self.r=r
def area(self):
ar=round(math.pi*self.r**2,2)
print("Area of the circle is ",ar)
def perimeter(self):
p=round(2*math.pi*self.r,2)
print("Perimeter of the circle is ",p)
#main program
rad=float(input("enter the radius of the circle\t"))
ob=Circle(rad)
ob.area()
ob.perimeter()

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 18


MCA 2018 - 2020 PYTHON LAB

PROGRAM 18
Program to perform basic calculator operations using class concept

SOURCE CODE

class Calculator:
def __init__(self,a,b):
self.a=a
self.b=b
def add(self):
print("The sum is ",self.a+self.b)
def sub(self):
print("The difference is ",self.a-self.b)
def div(self):
print("The quotient is ",self.a/self.b)
def mul(self):
print("The product is ",self.a*self.b)
#main program
print("==============SIMPLE CALCULATOR==========")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
print("4.Exit")
print("=======================================")
while True:
c=int(input("enter the choice\t"))
if c==5:
break
a=int(input("enter the first number\t"))
b=int(input("enter the second number\t"))
ob=Calculator(a,b)
if c==1:
ob.add()
elif c==2:
ob.sub()
elif c==3:
ob.mul()
elif c==4:
ob.div()
print("=======================================")

PG Dept of Computer Applications, Marian College Kuttikanam Page 19


MCA 2018 - 2020 PYTHON LAB

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 20


MCA 2018 - 2020 PYTHON LAB

PROGRAM 19
Program to read a text file and count the occurrence of a certain letter in that file

SOURCE CODE

fn=input("Enter the file\n")


l=input("enter the letter to count\n")
cnt=0
with open(fn,"r") as f:
for lines in f:
words=lines.split()
for word in words:
for letter in word:
if letter==l:
cnt+=1
print("The count of the letter is ",cnt)

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 21


MCA 2018 - 2020 PYTHON LAB

PROGRAM 20
Program to read a text file and print all digits present in it

SOURCE CODE

fn=input("Enter the file\n")


print("The digits in the file are ")
with open(fn,"r") as f:
for lines in f:
words=lines.split()
for word in words:
for letter in word:
if letter.isdigit():
print(letter)

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 22


MCA 2018 - 2020 PYTHON LAB

PROGRAM 21
Program for the generation of different pyramid patterns

SOURCE CODE

n=int(input("enter the limit"))


print("PYRAMID 1\n")
for i in range(n+1):
print(" "*(n-i),"* "*i)
print("PYRAMID 2\n")
for i in range(n,0,-1):
print(" "*(n-i),"* "*i)
print("PYRAMID 3\n")
for i in range(1,n+1,2):
print(" "*(n-i),"* "*i)
for i in range(n-2,0,-2):
print(" "*(n-i),"* "*i)
print("PYRAMID 4\n")
for i in range(1,n+1):
print(" "*(n-i),end="")
for j in range(1,i+1):
print(" ",j,end=" ")
print(" ")

PG Dept of Computer Applications, Marian College Kuttikanam Page 23


MCA 2018 - 2020 PYTHON LAB

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 24


MCA 2018 - 2020 PYTHON LAB

PROGRAM 22
Program to convert Celsius to Fahrenheit and vice versa

SOURCE CODE

print("==========TEMPERATURE CONVERSION==========")
print("1. Fahrenheit to celisius")
print("2. Celisius to Farenheit")
print("3. Exit")
print("==========================================")
while True:
c=int(input("enter the choice\t"))
if c==3:
break
l=int(input("enter lower limit of range\t"))
u=int(input("enter upper limit of range\t"))
if c==1:
print("The Celisius temperature")
for i in range(l,u):
cel=(5*(i-32))/9
print(round(cel,2))
if c==2:
print("The fahrenheit temperature")
for i in range(l,u):
f=(9*i+(32*5))/5
print(round(f,2))
print("==========================================")

PG Dept of Computer Applications, Marian College Kuttikanam Page 25


MCA 2018 - 2020 PYTHON LAB

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 26


MCA 2018 - 2020 PYTHON LAB

PROGRAM 23
Program to create a BMI calculator

SOURCE CODE

def bmi(w,h):
print("The body mass index is ",(w/h**2))
#main program
w=float(input("enter the weight in kilogram\n"))
h=float(input("enter the height in centimeters\n"))
hm=h/100
bmi(w,hm)

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 27


MCA 2018 - 2020 PYTHON LAB

PROGRAM 24
Program to read a text file and count the occurrence of a certain word in that file

SOURCE CODE

fn=input("Enter the file\n")


w=input("enter the word to count\n")
cnt=0
with open(fn,"r") as f:
for lines in f:
words=lines.split()
for word in words:
if word==w:
cnt+=1
print("The count of the word is ",cnt)

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 28


MCA 2018 - 2020 PYTHON LAB

PROGRAM 25
Program to calculate restaurant bill based on orders made

SOURCE CODE

print("==========RESTAURANT BILL==========")
print('Enter ordered items for person 1')
ap1 = float(input('Appetizier: '))
ep1= float(input('Main meal: '))
dp1 = float(input('Drinks: '))
des1 = float(input('Dessert: '))
print('\nEnter ordered items for person 2')
ap2 = float(input('Appetizier: '))
ep2 = float(input('Main meal: '))
dp2 = float(input('Drinks: '))
des2 = float(input('Dessert: '))
tot1 = ap1+ep1+dp1+des1
tot2 = ap2+ep2+dp2+des2
cost =tot1+tot2
print("Total bill amount = ",cost)
print("====================================")
c=input("Do you have the gift coupon?(y/n)\t")
if c=='y' or c=='Y':
gift= float(input('Enter amount of the gift coupon: '))
amt=gift-cost
if amt<0:
bill=-1*amt
print("Amount to be paid is ",bill)
else:
print("Nothing to pay, balance in gift coupon is ",amt)
else:
print("total amount to be paid is ",cost)
print("====================================")

PG Dept of Computer Applications, Marian College Kuttikanam Page 29


MCA 2018 - 2020 PYTHON LAB

OUTPUT

PG Dept of Computer Applications, Marian College Kuttikanam Page 30

You might also like