Sample Copy Python CS506
Sample Copy Python CS506
Output:-
Enter the first number: 23
Enter the second number: 22
The GCD of 23 and 22 is 1
[Type here]
EXPERIMENT- 02
Q2. To write a Python Program to find the square root of a number by Newton’s
Method.
ANS:
def newton_sqrt(n, tolerance=1e-10):
x=n
while True:
root = 0.5 * (x + n / x)
if abs(root - x) < tolerance:
return root
x = root
number = float(input("Enter a number to find the square root: "))
sqrt_value = newton_sqrt(number)
print(f"The square root of {number} is approximately {sqrt_value}")
output:-
Enter a number to find the square root: 144
[Type here]
EXPERIMENT-03
Description: To write a Python program to find the exponentiation of a number.
ANS:
result = 1
for _ in range(exponent):
result *= base
return result
Output:-
[Type here]
EXPERIMENT-04
Description: To write a Python Program to find the maximum from a list of numbers.
ANS:
def find_max(numbers):
max_num = numbers[0]
for num in numbers:
if num > max_num:
max_num = num
return max_num
numbers = list(map(int, input("Enter a list of numbers separated by spaces: ").split()))
max_value = find_max(numbers)
print(f"The maximum number in the list is: {max_value}")
Output-
Enter a list of numbers separated by spaces: 12 13 11 2 3 4 5 6 7 8 9
The maximum number in the list is: 13
[Type here]
EXPERIMENT-05
Description: write a program to implement linear search on list .
ANS:
if num == target:
return index
return -1
# Example usage
if result != -1:
else:
Output:-
Enter a list of numbers separated by spaces: 12 33 4 5 74 324 242 4245
Enter the number to search for: 5
Number found at index 3
[Type here]
EXPERIMENT-06
Description: Write a program to perform binary search
Solution:-
if result != -1:
print(f"Number found at index {result}")
else:
print("Number not found in the list")
Output:-
[Type here]
EXPERIMENT - 07
Description: Write a program to implement selection sort
Solution:-
def selection_sort(numbers):
for i in range(len(numbers)):
min_index = i
min_index = j
selection_sort(numbers)
Output:-
Enter a list of numbers separated by spaces: 1 22 13 1 24 22 2 34
[Type here]
EXPERIMENT - 08
Description: To write a Python Program to perform insertion sort.
ANS:
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j=i-1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr
arr = list(map(int, input("Enter numbers separated by spaces: ").split()))
sorted_arr = insertion_sort(arr)
print("Sorted array:", sorted_arr)
Output:-
Enter numbers separated by spaces: 12 11 13 5 6
Sorted array: [5, 6, 11, 12, 13]
[Type here]
EXPERIMENT - 09
Description :To write a Python Program to perform Merge sort.
ANS:
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]
merge_sort(left_half)
merge_sort(right_half)
i=j=k=0
[Type here]
j += 1
k += 1
return arr
Output:-
Enter numbers separated by spaces: 38 27 43 3 9 82 10
[Type here]
EXPERIMENT - 10
Description: Write a program to find first n prime numbers.
ANS:
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 = int(input("Enter the value of n: "))
prime_numbers = first_n_primes(n)
print("First", n, "prime numbers:", prime_numbers)
Output:-
Enter the value of n: 10
First 10 prime numbers: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
[Type here]
EXPERIMENT - 11
Description: Write a program to multiply two matrices.
ANS:
def take_input(rows, cols, matrix_name):
matrix = []
print(f"Enter {matrix_name} elements in a single line, space-separated:")
elements = list(map(int, input().split()))
for i in range(rows):
row = elements[i * cols:(i + 1) * cols]
matrix.append(row)
return matrix
[Type here]
Output:-
Enter Matrix A elements in a single line, space-separated:
123456
Enter Matrix B elements in a single line, space-separated:
7 8 9 10 11 12
Resultant Matrix after multiplication in a single array:
[58, 64, 139, 154]
[Type here]
EXPERIMENT - 12
Description: write a program for command line arguments.
ANS:
import sys
n = len(sys.argv)
Sum = 0
Sum += int(sys.argv[i])
print("\n\nResult:", Sum)
Output:-
[Type here]
EXPERIMENT - 13
Practical Description: write a program to count the most frequent word in a text file
Solution:-
count = 0;
word = "";
maxCount = 0;
words = [];
for s in string:
words.append(s);
count = 1;
if(words[i] == words[j]):
count = count + 1;
maxCount = count;
word = words[i];
file.close();
output:-
[Type here]
EXPERIMENT - 14
Practical Description: write a program to simulate elliptical orbit in pygame
Solution:-
import pygame
import math
import sys
pygame.init()
pygame.display.set_caption("Elliptical orbit")
clock=pygame.time.Clock()
while(True):
if event.type == pygame.QUIT:
sys.exit()
xRadius = 250
yRadius = 100
screen.fill((0, 0, 0))
pygame.draw.ellipse(screen,(255,255,255),[50,50,500,200],1)
pygame.display.flip()
[Type here]
Output:-
[Type here]
EXPERIMENT - 15
Practical Description: write a program for bouncing ball in pygame
Solution:-
pygame.init()
speed = [1, 1]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Bouncing ball")
ball = pygame.image.load("ball.png")
ballrect = ball.get_rect()
while 1:
if event.type == pygame.QUIT:
sys.exit()
ballrect = ballrect.move(speed)
[Type here]
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
speed[1] = -speed[1]
screen.fill(background)
screen.blit(ball, ballrect)
pygame.display.flip()
Output:-
For check the demo of bouncing ball program click the ball image above
[Type here]