[go: up one dir, main page]

0% found this document useful (0 votes)
68 views14 pages

Index: S.no. Name of The Experiment Date

The document contains a 12-item index describing experiments on various programming topics. Each item lists the name of the experiment, the page number it can be found on, and the date. The experiments include calculating the GCD and LCM of numbers, exponents, searching and sorting algorithms like linear search, binary search, selection sort, insertion sort, and merge sort, as well as math operations like matrix addition and multiplication.

Uploaded by

Aman Singh
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)
68 views14 pages

Index: S.no. Name of The Experiment Date

The document contains a 12-item index describing experiments on various programming topics. Each item lists the name of the experiment, the page number it can be found on, and the date. The experiments include calculating the GCD and LCM of numbers, exponents, searching and sorting algorithms like linear search, binary search, selection sort, insertion sort, and merge sort, as well as math operations like matrix addition and multiplication.

Uploaded by

Aman Singh
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/ 14

INDEX

S.no. Name of the Experiment Page Date


1 WAP to calculate GCD of 2 numbers 1 20/10/2020
2 WAP to calculate LCM of 2 numbers 2 20/10/2020
3 WAP to calculate exponent of a number 3 27/10/2020
4 WAP to find Biggest no. from a List 4 27/10/2020
5 WAP to do Linear Search in a List 5 03/11/2020
6 WAP to do Binary Search in a List using 6 03/11/2020
Recursion
7 WAP to demonstrate Selection Sort 7 09/11/2020
8 WAP to demonstrate Insertion Sort 8 09/11/2020
9 WAP to demonstrate Merge Sort 9-10 16/11/2020
10 WAP to find first n Prime Numbers 11 23/11/2020
11 WAP to find Matrix Addition 12 23/11/2020
12 WAP to do Matrix Multiplication 13 7/12/2020

Experiment -1
WAP to calculate GCD of 2 numbers:
#The Greatest common divisor (GCD) or highest common factor (HCF) of two numbers is the
largest positive integer that perfectly divides the two given members.
def GCD(X,Y):
if(X>Y):
s=Y
else:
s=X
for i in range(2,s+1):
if((X%i==0)and(Y%i==0)):
GCD=i
return GCD
else:
return 1
n1=int(input("enter first number:"))
n2=int(input("enter Second number:"))
print("The GCD is: ",GCD(n1,n2))

Experiment -2
1
WAP to calculate LCM of 2 numbers:
#The Least Common Multiple (L.C.M) of two numbers is the smallest positive integer that is perfectly
divisible by the two given numbers.
def compute_LCM(x, y):
if x > y:
greater = x
else:
greater = y
while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1
return lcm
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("The L.C.M. is", compute_LCM(num1, num2))

Experiment -3
2
WAP to calculate exponent of a number:
"""Algorithm:-
1. Define a function named power() 
2. Read the values of base and exp 
3. Use ‘if’ to check if exp is equal to 1 or not 
i. if exp is equal to 1, then return base 
ii. if exp is not equal to 1, then return  the following value
(base* power(base, exp-1)) 
4. Print the result. """
def power(base,exp):
if(exp==1):
return(base)
if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:", power(base, exp))

Experiment -4

3
WAP to find Biggest no. from a List:
"""Algorithm:-
1. Create an empty list named l
2. Read the value of n
3. Read the elements of the list until n
4. Assign l[0] as maxno
5. If l[i] > maxno then set maxno = l[i]
6. Increment i by 1
7. Repeat steps 5-6 until I < n
8. Print the value of maximum number """
l=[]
n = int( input("enter the upper limit"))
for i in range(0,n):
a=int(input("enter the numbers"))
l.append(a)
maxno=l[0]
for i in range(0, len(l)):
if l[i] > maxno:
maxno = l[i]
print("The maximum number is %d"%maxno)

Experiment -5

4
WAP to do Linear Search in a List:
#Searching means to find whether a particular value is present or not in the array.
data=[10,2,54,15,36,48]
print("list of item is",data)
x=(input("Enter item to search:"))
x=int(x)
i=0
flag=0
while i<len(data):
if data[i]==x:
flag=1
break
i=i+1
if flag==1:
print("Item is found at position:",i+1)
else:
print("Item not found")

Experiment -6

5
WAP to do Binary Search in a List using Recursion:
#This search algorithm works on the principle of divide and conquer. Pre-requisite for this algorithm
to work properly the data collection should be in sorted form.
def binary_search(arr,low,high,x):
if high>=low:
mid=int((high+low)/2)
if arr[mid]==x:
return mid+1
elif arr[mid]>x:
return binary_search(arr,low,mid-1,x)
else:
return binary_search(arr,mid+1,high,x)
else:
return -1
arr=[2,3,4,5,6,7,8,9,10]
x=7
result=binary_search(arr,0,len(arr)-1,x)
if result!=-1:
print("Element placed at index",str(result))
else:
print("Element not found")

Experiment -7
WAP to demonstrate Selection Sort:
6
"""Algorithm:
1. Create a function named selection sort
2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location
5. Swap alist[i] and alist[pos]
6. Print the sorted list
"""
def selectionsort(alist):
for i in range(len(alist)-1,0,-1):
pos=0
for location in range(1,i+1):
if alist[location]>alist[pos]:
pos=location
temp=alist[i]
alist[i]=alist[pos]
alist[pos]=temp
alist=[35,5,2,8,4,5,5546,643,316,12]
print("Before sort",alist)
selectionsort(alist)
print("After sort",alist)

Experiment -8
WAP to demonstrate Insertion Sort:
"""Algorithm:

7
1. Create a function named insertionsort
2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]
5. position = position-1
6. alist[position]=currentvalue
7. Print the sorted list
"""
def insertionsort(blist):
for i in range(1,len(blist)):
key=blist[i]
j=i-1
while j>0 and key<blist[j]:
blist[j+1]=blist[j]
j-=1
blist[j+1]=key
blist=[32,56,21,621,6316,212,43]
print("Before sort",blist)
insertionsort(blist)
print("After sort",blist)

Experiment -9
WAP to demonstrate Merge Sort:
#Merge Sort is a Divide and Conquer algorithm. It divides the input array into two halves, calls itself
for the two halves, and then merges the two sorted halves.

8
def merge(arr, l, m, r):
n1 = m - l + 1
n2 = r- m
L = [0] * (n1)
R = [0] * (n2)
for i in range(0 , n1):
L[i] = arr[l + i]

for j in range(0 , n2):


R[j] = arr[m + 1 + j]
i=0
j=0
k=l
while i < n1 and j < n2 :
if L[i] <= R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
while i < n1:
arr[k] = L[i]
i += 1
k += 1
while j < n2:
arr[k] = R[j]
j += 1
k += 1
def mergeSort(arr,l,r):
if l < r:
m = (l+(r-1))//2
mergeSort(arr, l, m)
mergeSort(arr, m+1, r)
merge(arr, l, m, r)
arr = [12, 11, 13, 5, 6, 7]
n = len(arr)
print ("Given array is")
for i in range(n):
print ("%d" %arr[i]),
mergeSort(arr,0,n-1)
print ("Sorted array is")

9
for i in range(n):
print ("%d" %arr[i]),

Experiment -10
WAP to find first n Prime Numbers:
"""Algorithm:
1. Read the value of n
2. for num in range(0,n + 1), perform the following
3. if num%i is 0 then break

10
else print the value of num
4. Repeat step 3 for i in range(2,num)
"""
numr=int(input("Enter range:"))
print("Prime numbers:",end=' ')
for n in range(1,numr):
for i in range(2,n):
if(n%i==0):
break
else:
print(n,end=' ')

Experiment -11
WAP to find Matrix Addition:
#Matrix addition in C language to add two matrices, i.e., compute their sum and print it. A user
inputs their orders (number of rows and columns) and the matrices.
X = [[12,7,3],[4 ,5,6],[7 ,8,9]]
Y = [[5,8,1],[6,7,3],[4,5,9]]
result = [[0,0,0],[0,0,0],[0,0,0]]

11
for i in range(len(X)):
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
for r in result:
print(r)

Experiment -12
WAP to do Matrix Multiplication:
#Matrix multiplication in C language to calculate the product of two matrices (two-dimensional
arrays). A user inputs the orders and elements of the matrices.
X = [[12,7,3],[4 ,5,6],[7 ,8,9]]
Y = [[5,8,1,2],[6,7,3,0],[4,5,9,1]]
result = [[0,0,0,0],[0,0,0,0],[0,0,0,0]]
for i in range(len(X)):
for j in range(len(Y[0])):

12
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
for r in result:
print(r)

13

You might also like