Ex:1 GCD OF TWO NUMBERS
AIM: To write a Python program to compute the GCD of two numbers.
ALGORITHM:
Step 1: Start.
Step 2: Read the value of first number n1.
Step 3: Read the value of second number n2 .
Step 4: Calculate rem=n1%n2.
Step 5: Repeat the Steps 6 to 7 until rem!=0.
Step 6: Assign n1=n2
. Step 7: Assign n2=rem.
Step 8: Calculate rem=n1%n2.
Step 9: Print the GCD of given numbers is n2.
Step 10: Stop
Program:
n1= int(input("enter a number:"))
n2=int(input("enter another number:"))
rem =n1%n2
while rem!=0:
n1 = n2
n2 = rem
rem = n1% n2
print ("gcd of the given number is :",n2)
OUTPUT:
Enter a number:54
Enter the another number:24
GCD of given number is:6
Enter a number:54
Enter the another number:45
GCD of given number is:9
Result:
Thus the Python program to compute GCD of two numbers is executed successfully and the output is
verified.
EX.NO.3 Exponentiation of a number
AIM:
To write a Python program to compute Exponentiation of a numbers.
ALGORITHM:
Step 1 : Start
Step 2 : Read the value for n.
Step 3 : Read the value for e.
Step 4 : Assign r=n.
Step 5 : Repeat step 4 until e occur false.
Step 6 : Calculate r=n*r
Step 7 : Print the value of r
. Step 8 : Stop.
Program:
n=int(input('enter number:'))
e=int(input('enter exponent:'))
r=n
for i in range(1,e):
r=n*r
print('exponentiation is:',r)
OUTPUT:
Enter the number :3
Enter the Exponent:2
Exponentiation is:9
Enter the number :2
Enter the Exponent:5
Exponentiation is:32
EX.No:4 Maximum number& Minimum number
list1=[]
print("Enter upper limit...")
n=int (input())
print ("Enter the numbers")
for i in range (0,n):
a=int (input(""))
list1.append(a)
maxno = list1[0]
minno = list1[0]
for i in range (len(list1)):
if list1[i] > maxno:
maxno = list1[i]
if list1[i] < minno:
minno = list1[i]
print('maximum number of the list:',maxno)
print('minimum number of the list:',minno)
OUTPUT:
Enter the Upper limit..
Enter the numbers
Maximum number of the list:5
Minimum number of the list:1
Ex.No.5a Linear SEARCH
AIM:
To write a python program to perform the linear search.
ALGORITHM:
Step 1: Start
Step 2: Read the element in list.
Step 3: Read the searching element from the user
Step 4: Assign to FALSE flag value
Step 5: Search the element with using for loop until length of list
Step 6: If value is found assign the flag value is true
Step7: Then print the output of founded value and position.
Step8: If value is not found then go to next step
Step9: Print the not found statement
PROGRAM/SOURCE CODE :
list = [14, 20, 58, 90, 3, 17]
x = int(input("Enter number to search: "))
found = False
for i in range(len(list)):
if(list[i] == x):
found = True
print("%d found at %dth position"%(x,i))
break
else:
print("%d is not in list"%x)
OUTPUT:
Enter number to search: 90
90 found at 4th position
Ex.No.5b BINARY SEARCH
AIM: To write a python program to perform the binary search.
ALGORITHM:
Binary_search [arr, start , last element]
Step:1- mid = (start+ last) / 2
Step:2- If start> last
Then, Print "Element not found"
Exit
Else if element >arr[mid]
Then, start = mid + 1
Go to Step:1
Else if element element < arr[mid]
Then, last = mid - 1
Go to Step:2
Else
{element == arr[mid] }
Print "Element Presented at position" + mid
exit
def Binary_search(arr,start,last,element):
mid =(int)(start+last)/2
if(start>last):
print "Element not found"
elif (element>arr[mid]):
start = mid+1
Binary_search(arr,start,last,element)
elif (element<arr[mid]):
last = mid-1
Binary_search(arr,start,last,element)
else:
print "element is present at index " + str(mid)
arr = [2,14,19,21,99,210,512,1028,4443,5110]
element = 4443
start = 0
last = len(arr)-1
Binary_search(arr,start,last,element)
Ex.No.6a SELECTION SORT
array = [1,45,10,35,100,13,147,500,80]
size = len(array)
for i in range(0,size):
for j in range(i+1,size):
if array[j] < array[i]:
min = array[j];
array[j] = array[i];
array[i] = min;
print(array)
(or)
a = [16, 19, 11, 15, 10, 12, 14]
i = 0
while i<len(a):
#smallest element in the sublist
smallest = min(a[i:])
#index of smallest element
index_of_smallest = a.index(smallest)
#swapping
a[i],a[index_of_smallest] = a[index_of_smallest],a[i]
i=i+1
print (a)
(OR)
1. def selectionSort(X):
2. for i in range(len(X)):
3. min_index = i
4. for j in range(i+1, len(X)):
5. if X[min_index] > X[j]:
6. min_index = j
7. X[i], X[min_index] = X[min_index], X[i]
8. return X
9. X = [36, 21, 12, 19, 5]
10. sorted_X = selectionSort(X)
11. print(sorted_X)
Ex.NO:6b INSERTION SORT
def insertion_sort(a):
for i in range(0, len(a)):
j=i
while((j>0) and (a[j-1]>a[j])):
a[j-1], a[j] = a[j], a[j-1]
j = j-1
if __name__ == '__main__':
a = [4, 8, 1, 3, 10, 9, 2, 11, 5, 6]
insertion_sort(a)
print(a)
OR
1. def insertionSort(array):
2.
3. for step in range(1, len(array)):
4. key = array[step]
5. j = step - 1
6. while j >= 0 and key < array[j]:
7.
8. # For descending order, change key<array[j] to key>array[j].
9.
10. array[j + 1] = array[j]
11. j = j - 1
12. array[j + 1] = key
13.
14.
15. data = [9, 5, 1, 4, 3]
16. insertionSort(data)
17. print('Sorted Array in Ascending Order:')
18. print(data)
EX.NO.7 MERGE SORT
def mergesort(alist):
print("splitting",alist)
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]
mergesort(lefthalf)
mergesort(righthalf)
i=0
j=0
k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j] :
alist[k] = lefthalf[i]
i=i+1
k=k+1
else:
alist[k] = righthalf[j]
j = j+1
k = k+1
while i <len(lefthalf):
alist[k] = lefthalf[i]
i = i+1
k = k+1
while j <len(righthalf):
alist[k] = righthalf[j]
j = j+1
k = k+1
print("merging",alist)
alist=[13,2,17,76,82,12,7]
mergesort (alist)
print (alist)
OUTPUT:
splitting [13, 2, 17, 76, 82, 12, 7]
splitting [13, 2, 17]
splitting [13]
merging [13]
splitting [2, 17]
splitting [2]
merging [2]
splitting [17]
merging [17]
merging [2, 17]
merging [2, 13, 17]
splitting [76, 82, 12, 7]
splitting [76, 82]
splitting [76]
merging [76]
splitting [82]
merging [82]
merging [76, 82]
splitting [12, 7]
splitting [12]
merging [12]
splitting [7]
merging [7]
merging [7, 12]
merging [7, 76, 82, 12]
merging [2, 13, 17, 7, 76, 82, 12]
[2, 13, 17, 7, 76, 82, 12]
EX.No:8 FIRST n PRIME NUMBERS
n=int(input('enter the upper limit:'))
for num in range(1,n):
for i in range(2,num):
if num%i == 0:
j=num/i
print('%d equals %d * %d'%(num,i,j))
break
else:
print(num, 'is a prime number')
OUTPUT:
Enter the upper limit:6
1 is a prime number
2 is a prime number
3 is a prime number
4 equals 2*2
5 is a prime number
6 equals 2*3
TO FIND THE FIRST n PRIME NUMBERS:
n=int(input(“enter a number r:”))
if n>1:
for i in range (2,n):
if (n%i )==0:
print(i,”presents time”,n//i,’is’,n)
print(n”, is not a prime number”)
break
else:
print(n,” is a prime number”)
else:
print(n,”is not a prime number”)
OUTPUT:
Enter a no :5
5 is a prime number
Enter a number :8
8is not a prime number
2times 4 to 8