PP Lab Sem
PP Lab Sem
a = 48
b = 18
print("GCD using for loop:", gcd_for_loop(a, b))
def find_max(lst):
max_num = lst[0]
for num in lst:
if num > max_num:
max_num = num
return max_num
numbers = [3, 5, 2, 8, 6]
print("Maximum number:", find_max(numbers))
2. a) Develop a python program to perform linear search in
a list
numbers = [3, 5, 2, 8, 6]
x = 8
print("Element found at index:", linear_search(numbers, x))
number = 16
print("Square root:", newton_sqrt(number))
3. a) Develop a python program to Compute the GCD of two
Numbers using functions
a = 48
b = 18
print("GCD using function:", gcd(a, b))
======================================================
base = 2
exp = 3
print("Exponentiation:", exponentiation(base, exp))
4. a) Develop a python program to to display the first n
prime numbers until the range n is given.
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
def first_n_primes(n):
primes = []
num = 2
while len(primes) < n:
if is_prime(num):
primes.append(num)
num += 1
return primes
n = 10
print("First n prime numbers:", first_n_primes(n))
def insertion_sort(lst):
for i in range(1, len(lst)):
key = lst[i]
j = i - 1
while j >= 0 and key < lst[j]:
lst[j + 1] = lst[j]
j -= 1
lst[j + 1] = key
def is_even_or_odd(num):
if num % 2 == 0:
return "Even"
else:
return "Odd"
number = 7
print("The number is:", is_even_or_odd(number))
7. a) Develop a python program to Program that takes the
most frequent words in a text file.
filename = "text.txt"
print("Most frequent words:", most_frequent_words(filename))
def multiplication_table(n):
for i in range(1, 11):
print(f"{n} x {i} = {n*i}")
number = 5
multiplication_table(number)
8. a) Develop a python program to Check whether the given
year is Leap Year or not
def is_leap_year(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False
year = 2024
print(f"The year {year} is a leap year:",
is_leap_year(year))
def is_eligible_to_vote(age):
return age >= 18
age = 20
print("Eligible to vote:", is_eligible_to_vote(age))
9. a) Develop a python program to Display Calendar
import calendar
year = 2024
month = 6
display_calendar(year, month)
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
number = 5
print("Factorial:", factorial(number))
10. a) Develop a python program to convert kilometre to
miles
def km_to_miles(km):
return km * 0.621371
kilometres = 5
print("Miles:", km_to_miles(kilometres))
base = 10
height = 5
print("Area of triangle:", triangle_area(base, height))
11. a) Develop a python program to print the fibbonacci
series
def fibonacci(n):
fib_series = [0, 1]
for i in range(2, n):
fib_series.append(fib_series[-1] + fib_series[-2])
return fib_series[:n]
n = 10
print("Fibonacci series:", fibonacci(n))
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
number = 5
print("Factorial:", factorial(number))