0% found this document useful (0 votes)
13 views

PP Lab Sem

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

PP Lab Sem

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

1.

a) Develop a python program to Compute the GCD of two Numbers


using for Loop

def gcd_for_loop(a, b):


gcd = 1
for i in range(1, min(a, b) + 1):
if a % i == 0 and b % i == 0:
gcd = i
return gcd

a = 48
b = 18
print("GCD using for loop:", gcd_for_loop(a, b))

1. b) Write a python program to find the maximum of a list of


numbers

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

def linear_search(lst, x):


for i in range(len(lst)):
if lst[i] == x:
return i
return -1

numbers = [3, 5, 2, 8, 6]
x = 8
print("Element found at index:", linear_search(numbers, x))

2. b) Write a python program find square root using


newton’s method
def newton_sqrt(n, tolerance=1e-10):
guess = n / 2.0
while True:
new_guess = (guess + n / guess) / 2
if abs(new_guess - guess) < tolerance:
return new_guess
guess = new_guess

number = 16
print("Square root:", newton_sqrt(number))
3. a) Develop a python program to Compute the GCD of two
Numbers using functions

def gcd(a, b):


while b:
a, b = b, a % b
return a

a = 48
b = 18
print("GCD using function:", gcd(a, b))

======================================================

3. b) Write a python program find the exponentiation of a


number

def exponentiation(base, exp):


return base ** exp

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))

4. b) Write a python program to display the Product of


def matrix_product(A, B):
result = [[0 for _ in range(len(B[0]))] for _ in
range(len(A))]
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
result[i][j] += A[i][k] * B[k][j]
return result
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
print("Product of matrices:", matrix_product(A, B))
5. a) Develop a python program with help of Command Line
import sys
if len(sys.argv) > 1:
filename = sys.argv[1]
with open(filename, 'r') as file:
text = file.read()
word_count = len(text.split())
print("Word count:", word_count)
5. b) Write a python program which performs merge sort.
def merge_sort(lst):
if len(lst) > 1:
mid = len(lst) // 2
left_half = lst[:mid]
right_half = lst[mid:]
merge_sort(left_half)
merge_sort(right_half)
i = j = k = 0
while i < len(left_half) and j < len(right_half):
if left_half[i] < right_half[j]:
lst[k] = left_half[i]
i += 1
else:
lst[k] = right_half[j]
j += 1
k += 1
while i < len(left_half):
lst[k] = left_half[i]
i += 1
k += 1
while j < len(right_half):
lst[k] = right_half[j]
j += 1
numbers = [38, 27, 43, 3, 9, 82, 10]
merge_sort(numbers)
print("Sorted list:", numbers)
6. a) Develop a python program to program which performs
insertion sort.

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

numbers = [12, 11, 13, 5, 6]


insertion_sort(numbers)
print("Sorted list:", numbers)

6. b) Write a python program to Check if a Number is Odd


or Even

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.

from collections import Counter

def most_frequent_words(filename, n=10):


with open(filename, 'r') as file:
text = file.read()
words = text.split()
word_counts = Counter(words)
return word_counts.most_common(n)

filename = "text.txt"
print("Most frequent words:", most_frequent_words(filename))

7. b) Write a python program to Display the


multiplication Table

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))

8. b) Write a python program to check vote eligibility of


a person

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

def display_calendar(year, month):


print(calendar.month(year, month))

year = 2024
month = 6
display_calendar(year, month)

9. b) Write a python program to find the factorial of a


number

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))

10. b) Write a python program to find the area of a


triangle

def triangle_area(base, height):


return 0.5 * base * height

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))

11. b) Write a python program to find the factorial of a


number

def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)

number = 5
print("Factorial:", factorial(number))

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy