0% found this document useful (0 votes)
18 views13 pages

It PDF Merged

Uploaded by

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

It PDF Merged

Uploaded by

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

PROGRAM-4

Aim:- Write a program for Linear search

Tool Used:- Visual Studio Code Editor


Source Code:-
def linearSearch(num, x):
for idx in num:
if idx == x:
return f"The number {x} has been found at location {num.index(idx) + 1}"
return f"The number {x} has not been found"

num = list(map(int, input("Enter all elements with spaces: ").split()))

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

sequentially until the desired element is found or the list ends.


PROGRAM-5
Aim:- Write a program for Binary search

Tool Used:- Visual Studio Code Editor


Source Code:-
def binarySearch(arr,x):
lo=0
hi=len(arr)-1
while lo<=hi:
mid=(lo+hi)//2
if arr[mid]==x:
return mid
elif arr[mid]<x:
lo=mid+1
else:
hi=mid-1
return -1
num=list(map(float,input("Enter all elements with space: ").split()))
sortedNum=sorted(num)
x=float(input("Enter the number which you want to search in list: "))
res=binarySearch(sortedNum,x)
loc=num.index(sortedNum[res])
if res!=-1:
print("The number has found at index",loc)
else:
print("The number has not found")
OUTPUT:-

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

A calculator program allows a user to perform basic arithmetic operations such as


addition, subtraction, multiplication, and division. Users input two numbers and select an
operation, and the program outputs the result.
PROGRAM-1
Aim: Write a program to compute GCD of two numbers
Tool used: Visual Studio Code Editor

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

both numbers without leaving a remainder.


PROGRAM-3
Aim:- Write a program to find the maximum of a list of numbers

Tool Used:- Visual Studio Code Editor


Source Code:-
def maxNumber(list):
max=0
for i in list:
if i>max:
max=i
else:
continue
return max
a=list(map(int,input("Enter all elements with spaces: ").split()))
x=maxNumber(a)
if a!=None:
print("Maximum number in entered list: ",x)
else:
print("The list is empty")

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

be used, but implementing it manually provides a better understanding of how the

process works.
PROGRAM-6
Aim:- Write a program for Insertion sort

Tool Used:- Visual Studio Code Editor


Source Code:-
def insertionSort(a):
for i in range(1,len(a)):
c=a[i]
j=i-1
while j>=0 and a[j]>c:
a[j+1]=a[j]
j=j-1
a[j+1]=c
return a
Num=list(map(int,input("Enter all elements with spaces: ").split()))
Result=insertionSort(Num)
print("The sorted array is ",Result)
OUTPUT:-

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

in the correct position.


PROGRAM-7
Aim:- Write a program for Selection sort

Tool Used:- Visual Studio Code Editor


Source Code:-
def selectionSort(a):
for i in range(len(a)):
min=i
for j in range(i+1,len(a)):
if a[j]<a[min]:
min=j
a[i],a[min]=a[min],a[i]
return a
Num=list(map(int,input("Enter all elements with spaces: ").split()))
Result=selectionSort(Num)
print("The sorted array is ",Result)
OUTPUT:-

OUTPUT LEARNING:-

Selection Sort is a simple comparison-based sorting algorithm. It works by repeatedly selecting

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

# Input: number of prime numbers to find


n = int(input("Enter the number of prime numbers to find: "))
print("The first", n, "prime numbers are:", find_primes(n))
OUTPUT:-
PROGRAM-9
Aim:- Write a program to multiply matrices
Tool Used:- Visual Studio Code Editor
Source Code:-
def multiply_matrices(matrix1, matrix2):
rows_matrix1 = len(matrix1)
cols_matrix1 = len(matrix1[0])
rows_matrix2 = len(matrix2)
cols_matrix2 = len(matrix2[0])

if cols_matrix1 != rows_matrix2:
raise ValueError("Matrix multiplication not possible. Columns of matrix1 must
equal rows of matrix2.")

result = [[0 for x in range(cols_matrix2)] for x in range(rows_matrix1)]

# Perform matrix multiplication


for i in range(rows_matrix1):
for j in range(cols_matrix2):
for k in range(cols_matrix1):
result[i][j] += matrix1[i][k] * matrix2[k][j]

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

You might also like