It PDF Merged
It PDF Merged
if not num:
print("The list is empty")
else:
x = int(input("Enter the number you want to search in the list of numbers: "))
result = linearSearch(num, x)
print(result)
OUTPUT:-
OUTPUTLEARNIG-
Linear search is a simple search algorithm that checks each element of an array
OUTPUTLEARNIG-
Binary search is a highly efficient algorithm for finding an element in a sorted array. It
works by repeatedly dividing the search interval in half.
PROGRAM-2
Aim: Write a program for calculator
Tool used: Visual Studio Code Editor
Source Code:-
def calculator(a,op,b):
if op=='+':
return a+b
elif op=='-':
return a-b
elif op=='*':
return a*b
elif op=='^':
return a**b
elif op=='/':
if b==0:
return "Not Defined"
else:
return a/b
else:
return "Invalid Operator. Please enter valid operator"
while True:
try:
a=float(input("Enter first number:"))
op=input("Enter operand(+,-,*,/,^):")
b=float(input("Enter second number: "))
except ValueError:
print("Please enter valid input(number)")
continue
print("The value of ",a,op,b," is ",calculator(a,op,b))
query=input("Enter Y for next calculation if any otherwise enter N:")
if query=='Y':
continue
else:
break
OUTPUT:-
Output Learning:-
Source Code:-
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
for i in range(1,min(a,b)+1):
if a%i==0 and b%i==0:
gcd=i
else:
continue
print("The GCD of ",a," and ",b," is ",gcd)
OUTPUT:-
Output Learning:-
The GCD (Greatest Common Divisor) of two numbers is the largest number that divides
OUTPUT:-
OUTPUT
LEARNING:-
To find the maximum value in a list of numbers, we iterate through the list and compare
each number to the current maximum. Alternatively, Python's built-in max() function can
process works.
PROGRAM-6
Aim:- Write a program for Insertion sort
OUTPUT
LEARNING:-
Insertion sort is a straightforward sorting algorithm that builds a sorted array one element
at a time by comparing each new element to the already sorted elements and inserting it
OUTPUT LEARNING:-
the smallest (or largest) element from the unsorted portion of the array and swapping it with the
first unsorted element. This process continues until the entire array is sorted
PROGRAM-8
Aim:- Write a program to find first n prime numbers
Tool Used:- Visual Studio Code Editor
Source Code:-
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 find_primes(n):
primes = []
candidate = 2
while len(primes) < n:
if is_prime(candidate):
primes.append(candidate)
candidate += 1
return primes
if cols_matrix1 != rows_matrix2:
raise ValueError("Matrix multiplication not possible. Columns of matrix1 must
equal rows of matrix2.")
return result
matrix1 = [
[9, 5, 2],
[11, 1, 4],
]
matrix2 = [
[5, 1],
[3, 13],
[1, 2],
[2,2]
]
try:
print("Matrix one:")
for row in matrix1:
print(row)
print("Matrix Two:")
for row in matrix2:
print(row)
result = multiply_matrices(matrix1, matrix2)
print("Result of matrix multiplication:")
for row in result:
print(row)
except ValueError as e:
print(e)
OUTPUT:-
Output Learning:-
Through this program we learn how to traverse through a 2-D matrix and also how can
we multiply two 2-D matrix and also pre-requisite for multiplying two matrices.
OUTPUT LEARNING:-
This program helps learners understand prime numbers and how to identify them using Python.
It teaches control structures like for and while loops, function creation, and optimizing
algorithms (e.g., checking divisibility up to the square root).