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

UNIT III IP

The document contains several Python programs demonstrating basic algorithms including finding the GCD, calculating square roots, exponentiation, linear search, binary search, and summing an array of numbers. Each program includes user input and outputs the results based on the provided examples. The document serves as a practical guide for implementing these algorithms in Python.

Uploaded by

NivedhithaV
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)
3 views

UNIT III IP

The document contains several Python programs demonstrating basic algorithms including finding the GCD, calculating square roots, exponentiation, linear search, binary search, and summing an array of numbers. Each program includes user input and outputs the results based on the provided examples. The document serves as a practical guide for implementing these algorithms in Python.

Uploaded by

NivedhithaV
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/ 3

UNIT III

1. python program to find the gcd.

x=int(input("Enter X Value"))
y=int(input("Enter Y value"))
rem=x%y
while rem!=0:
x=y
y=rem
rem=x%y
print("GCD of given numbers is:",y)

OUTPUT:

Enter X Value54
Enter Y value24
GCD of given numbers is: 6

2. python program for finding the square root of a number.

def newtonSqrt(n, times):


approx = 0.5 * n
for i in range(times):
approx = 0.5 * (approx + n/approx)
return approx

print(newtonSqrt(25, 3))
print(newtonSqrt(25, 5))
print(newtonSqrt(25, 10))

Output:
5.01139410653
5.00000000002
5.0
3.python program to find the exponential value of a number.
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))

OUTPUT:

Enter base: 2
Enter exponential value: 3
Result: 8

4.python program for linear search.


list=[4,1,2,5,3]
search=int(input("Enter search number"))
for i in range(0,len(list)):
if search==list[i]:
print(search,"found at position",i)
break
else:
print("Not Found")

OUTPUT:

Enter search number3


3found at position4

Enter search number8


Not Found

5. python program for binary search.


def binarySearch (list, left, right, x):
if right >= left:
mid = (left + right) //2
if list[mid] == x:
return mid
elif list[mid] > x:
return binarySearch(list, left, mid-1, x)
else:
return binarySearch(list, mid+1, right, x)
else:
return -1

list = [ 2, 3, 4, 10, 40 ]
x = int(input("Enter the search element"))
result = binarySearch(list, 0, len(list)-1, x)
if result != -1:
print ("Element is present at index ", result)
else:
print ("Element is not present in array")

OUTPUT:

Enter the search element40


Element is present at index 4

Enter the search element50


Element is not present in array

6.python program for sum of an array of numbers.

def sum(arr):
total=0
for i in arr:
total=total+i
return total
arr=list(input("Enter the array of numbers"))
tot=sum(arr)
print("sum of array=",tot)

Output:
Enter the array of numbers 4,5,6
sum of array=15)

You might also like