0% found this document useful (0 votes)
4 views8 pages

Python Programs for Basic Algorithms

The document contains a series of Python lab exercises that cover various programming concepts, including finding the largest and smallest numbers, checking for Armstrong numbers, calculating factorials, generating Fibonacci series, and working with prime numbers. Additional exercises involve calculating areas and volumes, payroll calculations, and string manipulations. Each lab includes code snippets that demonstrate the implementation of these concepts.

Uploaded by

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

Python Programs for Basic Algorithms

The document contains a series of Python lab exercises that cover various programming concepts, including finding the largest and smallest numbers, checking for Armstrong numbers, calculating factorials, generating Fibonacci series, and working with prime numbers. Additional exercises involve calculating areas and volumes, payroll calculations, and string manipulations. Each lab includes code snippets that demonstrate the implementation of these concepts.

Uploaded by

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

lab 1.

1
#Largest among three numbers
a,b,c=eval(input("Enter three numbers:"))
if(a>b and a>c):
print(f"{a} is big")
elif(b>c):
print(f"{b} is big")
else:
print(f"{c} is big")

lab1.2
#Smallest among three numbers
a,b,c=eval(input("Enter three numbers:"))
if(a<b and a<c):
print(f"{a} is small")
elif(b<c):
print(f"{b} is small")
else:
print(f"{c} is small")

######lab1.3
#Armstrong number
n=eval(input("Enter a number:"))
m=n
s=0
while(n>0):
r=n%10
s=s+r**3
n=n//10
if(s==m):
print(f"{m} is Armstrong number")
else:
print(f"{m} is not Armstrong number")

######lab 1.4
#To find Armstrong number in an interval
a,b=eval(input("Enter range(start,end):"))
f=0
for n in range(a,b+1):
m=n
s=0
while(n>0):
r=n%10
s=s+r**3
n=n//10
if(s==m):
print(f"{m}")
else:
f=f+1
if(f==b-a-1):
print(f"There is no Armstrong number in the range {a},{b}")
#####lab 1.5
#Prime
n=eval(input("Enter a number:"))
f=0
for i in range(2,n):
if(n%i==0):
f=1
if(f==0):
print(f"{n} is prime")
else:
print(f"{n} is not prime")

#####lab1.6
#Prime in range
a,b=eval(input("Enter range(start,end):"))
for n in range(a+1,b):
f=0
for i in range(2,n):
if(n%i==0):
f=1
if(f==0):
print(f"{n}")

#####lab 1.7
#Factorial
n=int(input("Enter number:"))
s=1
for i in range(1,n+1):
s=s*i
print(f"Factorial of {n} is {s}")

###lab 1.8
#Fibonacci series
n=int(input("Enter no. of terms required:"))
if(n<=1):
print("0")
if(n==2):
print("0\t1")
if(n>=2):
print("0\n1")
a=0
b=1
for i in range(3,n+1):
c=a+b
print(f"{c}")
a=b
b=c

####lab 2
#area and volume of cylinder
r,l=eval(input("Enter radius and length of the cylinder:"))
area=2*3.14*r*l
volume=3.14*r**2*l
print(f"Area is {round(area,2)}\nVolume is {volume:.2f}")

####lab 3
#Best package
weight1,price1=eval(input("Enter weight and price for package 1:"))
weight2,price2=eval(input("Enter weight and price for package 2:"))
cost1=price1/weight1#price per unit weight of package1
cost2=price2/weight2#price per unit weight of package2
if(cost1<cost2):
print("Package 1 has the better price")
else:
print("Package 2 has the better price")

#####lab c4
#Display in increasing order

a,b,c=eval(input("Enter three integers:"))


if(a>b and a>c):
a,c=c,a
if(a>b):
a,b=b,a
if(b>a and b>c):
b,c=c,b
if(a>c):
a,c=c,a
if(c>a and c>b):
if(a>b):
a,b=b,a
print(a,b,c)

####lab 5
#divisibility by 5 and 6
num=int(input("Enter an integer:"))
print(f"{num} is divisible by both 5 and 6\t",num%5==0 and num%6==0)
print(f"{num} is divisible by 5 or 6\t",num%5==0 or num%6==0)
print(f"{num} is divisible by 5 or 6, but not by both\t",num%5==0 and num%6!=0

####lab 6
#Payroll
empName=str(input("Enter employee name:"))
hoursWorked=eval(input("Enter number of hours worked in a week:"))
payRate=eval(input("Enter hourly pay rate:"))
fdTaxRate=eval(input("Enter federal tax withholding rate(in %):"))/100
stTaxRate=eval(input("Enter state tax withholding rate(in %):"))/100
grossPay=hoursWorked*payRate
fdWh=fdTaxRate*grossPay
stWh=stTaxRate*grossPay
totalDeduction=fdWh+stWh
netPay=grossPay-totalDeduction
print(f"\nEmployee name:{empName}\nPay rate:{payRate}\nGross pay:${grossPay}")
print("Deductions:")
print(f"\tFederal withholding({fdTaxRate:.1%}):${fdWh:.1f}")
print(f"\tState withholding({stTaxRate:.1%}):${stWh:.2f}")
print(f"\tTotal deduction:${totalDeduction:.2f}")
print(f"Net pay:${netPay:.2f}")

#####lab 7
#Calculate interest
bal,intRate=eval(input("Enter balance and interest rate(annual):"))
interest=(intRate/(12*100)*bal)
print("Interest is",format(interest,".5f"))

####lab 8
n=eval(input("Enter a number(0 to quit):"))
max=n
count=0
while n!=0:
if(n==max):
count+=1
if(n>max):
max=n
count=1
n=eval(input("Enter a number(0 to quit):"))
print(f"The largest number is {max}")
print(f"The occurence count of largest number is {count}")

####lab 9
#Prime factors
num=eval(input("Enter an integer:"))
for i in range (2,num):
for j in range(2,i):
c=0
if j%i==0:
c=1
if c==0:
if num%j==0:
print(j)
num=num//j

####lab 10
#Patterns
n=6
for i in range(1,n+1):
for j in range(1,i+1):
print(j,"",end="")
print("\n")
n=6
for i in range(1,n+1):
for j in range(1,n-i+2):
print(j,"",end="")
print("\n")
n=6
for i in range(1,n+1):
for j in range(n-i):
print(" ",end="")
for k in range(i,0,-1):
print(k,"",end="")
print("\n")

#####l;ab11
lower_limit = int(input("Enter the lower limit of the range: "))
upper_limit = int(input("Enter the upper limit of the range: "))
divisor = int(input("Enter the number to check divisibility: "))
print(f"Numbers between {lower_limit} and {upper_limit} that are divisible by
{divisor}:")
for num in range(lower_limit, upper_limit + 1):
if num % divisor == 0:
print(num)

#####lab12
a=eval(input("enter the a value"))
b=eval(input("enter the b value"))
c=eval(input("enter the c value"))
d=((b*b)-(4*a*c))**0.5
if d>0:
print("roots are real")
root1=(-b+d)/(2*a)
root2=(-b-d)/(2*a)
print("root1=",root1,"root2=",root2)
elif d==0 :
print("roots are equal")
root=-b/(2*a)
else :
print("roots are imaginnary")
#####lab13

#Multiplication table
print("\t Multiplication table")
print(" |",end="")
for i in range(1,10):
print(" ",i,end="")
print()
print("----------------------------------------")
for i in range(1,10):
print(i,"|",end="")
for j in range(1,10):
print(format(i*j,"4d"),end="")
print()

#####lab14
name=input("enter the string ")
digits = 0
letters = 0
for char in name:
if [Link]():
digits += 1
elif [Link]():
letters+= 1
print(f"the given string{name}")
print("Number of digits:", digits)
print("Number of letters:", letters)

####lab 15
import random
head=0
tail=0
n=eval(input("enter the number of flips"))
for i in range (n):
flip=[Link](0,1)
if(flip==0):
tail+=1
else:
head+=1
print(f"Simulating {n} coin flips:")
print(f"Number of heads: {head}")
print(f"Number of tails: {tail}")
#####lab 16
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True

def is_palindrome(num):
return str(num) == str(num)[::-1]

def find_palindromic_primes(start, end):


print(f"Palindromic Prime Numbers between {start} and {end} are:")
for num in range(start, end + 1):
if is_prime(num) and is_palindrome(num):
print(num, end=' ')
print()

start_range = eval(input("Enter the start of the range: "))


end_range = eval(input("Enter the end of the range: "))

find_palindromic_primes(start_range, end_range)

####lab17
month = int(input("Enter a month (1-12): "))
year = int(input("Enter a year: "))
days = 0
if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month ==
10 or month == 12:
days = 31
elif month == 4 or month == 6 or month == 9 or month == 11:
days = 30
elif month == 2:
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
days = 29
else:
days = 28
else:
print("Invalid month")
if days > 0:
month_names = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"]
print(f"{month_names[month - 1]} {year} has {days} days.")
###lab18
input_string =input("enter the string ")
reversed_string = ""
for char in input_string:
reversed_string = char + reversed_string
print("Reversed String:", reversed_string)

#####lab19
def count(s):
upper_count=0
lower_count=0
for char in s:
if [Link]():
upper_count+=1
elif [Link]():
lower_count+=1
return upper_count,lower_count
def main():
input_string=input("enter the string :")
upper,lower=count(input_string)
print("original string :",input_string)
print("no of upper case characters :",upper)
print("no of lower case characters:",lower)
main()

#####lab 20
import random

def print_matrix(n):
for i in range (n):
for j in range (n):
print([Link](0,1),end=' ')
print()
def main():
n=eval(input("enter the matrix size"))
print_matrix(n)
main()

You might also like