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

Python

This document contains 4 experiments on algorithms in Python: 1) Write a program to compute GCD of two numbers using the hcf function. 2) Write a program to compute exponentiation of a number using a for loop. 3) Write a program to find the maximum and minimum elements in a list using the max() and min() functions. 4) Write programs to implement linear search and binary search on a list. Linear search sequentially checks each element while binary search divides the list in half at each step.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Python

This document contains 4 experiments on algorithms in Python: 1) Write a program to compute GCD of two numbers using the hcf function. 2) Write a program to compute exponentiation of a number using a for loop. 3) Write a program to find the maximum and minimum elements in a list using the max() and min() functions. 4) Write programs to implement linear search and binary search on a list. Linear search sequentially checks each element while binary search divides the list in half at each step.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

JSS Academy of Technical Education, Noida Department of Information Technology

EXPERIMENT NO. - 1

OBJECTIVE: Write a program to compute GCD of two numbers.

DESCRIPTION: The greatest common divisor (GCD) of two numbers is the largest
positive integer that divides both of the given numbers without leaving a remainder. In
other words, it is the largest number that is a common factor of both numbers.

CODE :

def hcf(a, b):


if(b == 0):
return a
else:
return hcf(b, a % b)

print("Enter the numbers")


x=int(input())
y=int(input())
gcd = hcf(x,y)
print("Gcd of",x,"and",y,"is: ",end="")
print(gcd)

OUTPUT :

Anand Pratap Singh 2100910130016 IT1 (A1)


JSS Academy of Technical Education, Noida Department of Information Technology

EXPERIMENT NO. - 2

OBJECTIVE: Write a program to compute Exponentiation of a number.

DESCRIPTION: Exponentiation of a number refers to the process of raising a base


number to a certain power, which is represented by an exponent. The exponent
tells us how many times the base number should be multiplied by itself.

Code:

print("Enter the value of base and exponent")


base=int(input())
exponent=int(input())
expo = exponent

result = 1

for expo in range(expo, 0, -1):


result *= base

print(base,"raised to power",exponent,"is equal to: ",str(result))

Output:

Anand Pratap Singh 2100910130016 IT1 (A1)


JSS Academy of Technical Education, Noida Department of Information Technology

EXPERIMENT NO. - 3

OBJECTIVE: Write a program to find maximum and minimum elements in a


List .

DESCRIPTION: In Python, a list is an ordered collection of elements enclosed in


square brackets ([]). To find the maximum and minimum elements in a list, you
can use the built-in functions max() and min() respectively

Code:

def max_value(list):

max = list[0]
min = list[0]
for i in list:
if i > max:
max = i
if i < min:
min = i

print("Max value in list is: ",max)


print("Min value in list is: ",min)

lst = []

n = int(input("Enter number of elements : "))

for i in range(0, n):


ele = int(input())
lst.append(ele)

max_value(lst)

Anand Pratap Singh 2100910130016 IT1 (A1)


JSS Academy of Technical Education, Noida Department of Information Technology

Output:

Anand Pratap Singh 2100910130016 IT1 (A1)


JSS Academy of Technical Education, Noida Department of Information Technology

EXPERIMENT NO. - 4

OBJECTIVE: Write a program to implement Linear search and Binary search .

DESCRIPTION: Linear search, also known as sequential search, is a simple


search algorithm that sequentially checks each element in a list or array until the
desired element is found or the end of the list is reached . Binary search is a more
efficient search algorithm that works on sorted lists or arrays. It repeatedly divides
the search space in half by comparing the middle element of the list with the target
element.

CODE :

def linearSearch(arr, x):

for i in range(len(arr)):

if arr[i] == x:
return i

return -1

def binary_search(arr, x):


low = 0
high = len(arr) - 1
mid = 0

while low <= high:


mid = (high + low) // 2

if arr[mid] < x:
low = mid + 1

elif arr[mid] > x:


high = mid - 1

Anand Pratap Singh 2100910130016 IT1 (A1)


JSS Academy of Technical Education, Noida Department of Information Technology

else:
return mid

return -1

lst = []

n = int(input("Enter number of elements : "))

for i in range(0, n):


ele = int(input())
lst.append(ele)

x = int(input("Enter the element you want to search: "))

print("Linear Search:")
if(linearSearch(lst,x)== -1):
print("Element is not found in the list")

else:
print("Element is found at index: ",linearSearch(lst,x))

print("Binary Search: ")


if(binary_search(lst,x)== -1):
print("Element is not found in the list")
else:
print("Element is found at index: ",binary_search(lst,x))

Anand Pratap Singh 2100910130016 IT1 (A1)


JSS Academy of Technical Education, Noida Department of Information Technology

Output:

Anand Pratap Singh 2100910130016 IT1 (A1)

You might also like