Professional Documents
Culture Documents
DAL Lab Manual
DAL Lab Manual
DAL Lab Manual
Aim:
To write aPython program to find GCD oftwo numbers.
Algorithm:
1. Defineafunction named compute GCD()
2. Find thesmallest amongthe two inputs xandy
3. Perform the following step tillsmaller+1
Check if ((x% i ==0)and (y%i ==0)), then assign GCD=i
4. Print thevalue ofgcd
Program:
defcomputeGCD(x,y):
if x>y:
smaller =y
else:
smaller =x
fori in range(1, smaller+1):
if((x% i ==0) and(y%i==0)):
gcd=i
returngcd
num1 = 54
num2 = 24
# takeinputfrom the user
# num1 = int(input("Enter first number:"))
# num2 = int(input("Enter second number: "))
print("TheGCD. of", num1,"and", num2,"is", computeGCD(num1, num2))
Sample Output:
$pythonmain.py
('TheGCD. of', 54,'and',24, 'is', 6)
Program2:WriteaPythonProgramtofindthesquarerootofanumberby
Newton’sMethod
Aim:
To write aPython Program to find thesquare rootofanumberbyNewton’sMethod.
Algorithm:
1. Defineafunction named newtonSqrt().
2.Initialize approxas 0.5*n and better as 0.5*(approx.+n/approx.)
3. Useawhile loop witha condition better!=approxto perform the following, i.
Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print thevalue ofapprox..
Program:
defnewtonSqrt(n):
approx=0.5 * n
better =0.5 * (approx+n/approx)
while better != approx:
approx=better
better =0.5 * (approx+n/approx)
returnapprox
print('Thesquare root is' ,newtonSqrt(100))
Sample Output:
Thesquare root is 10
Program3: Write aPython programto find theexponentiation ofanumber.
Aim:
To write aPython program to find the exponentiation of anumber.
Algorithm:
1. Defineafunction named power()
2. Read the values ofbase and exp
3. Use ‘if’to check ifexp is equal to 1 ornot
i. if exp is equal to 1, then return base
ii.ifexp is not equal to 1,then return (base*power(base,exp-1))
4. Print the result.
Program:
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))
Sample Output:
Enter base: 7
Enter exponential value: 2
Result:49
Program 4: Write a Python Program to find the maximum from a list of numbers.
Aim:
To write aPython Program to find themaximum from a list of numbers.
Algorithm:
1. Create anemptylist named l
2. Read the value of n
3. Read the elements of thelistuntil n
4. Assign l[0]as maxno
5.If l[i]>maxno then set maxno=l[i]
6.Increment iby1
7. Repeat steps 5-6 until i<n
8. Print thevalue ofmaximum number
Program:
l=[]
n=int(input("enter theupper limit"))
fori in range(0,n):
a=int(input("enterthenumbers"))
l.append(a)
maxno=l[0]
fori in range(0,len(l)):
if l[i]>maxno:
maxno=l[i]
print("Themaximum numberis %d"%maxno)
SampleOutput:
$pythonmain.py
(list of items is: [5, 7, 10, 12, 15])
enteritem to search:7 (item
found at position:, 2)
Program6: Write aPythonProgramtoperformBinarySearch
Aim:
To write aPython Program to perform binarysearch.
Algorithm:
1. Read the search element
2. Find themiddle element in the sorted list
3. Comparethe searchelement with the middle element
i. if both arematching, printelement found
ii. else then check if thesearchelement is smallerorlarger than the middleelement
4.If thesearchelement is smaller than the middle element, then repeat steps 2 and 3 for the
left sublistof themiddle element
5.If thesearchelement is larger than themiddleelement, then repeat steps2 and 3 forthe
right sublist of the middle element
6. Repeat theprocess untilthe searchelement if found in thelist
7.If element is not found, loop terminates
Program:
# Pythoncodeto implement iterativeBinarySearch.
#Itreturns location ofxin givenarrayarr
# if present, elsereturns-1
defbinarySearch(arr, l, r, x):
while l <= r:
mid =l + (r -l)/2;
# Check if xis present at mid
if arr[mid]==x:
return mid
#If xis greater, ignoreleft half
elifarr[mid]<x:
l = mid + 1
#If xis smaller, ignoreright half
else:
r =mid -1
#Ifwereach here, then the element
# was not present
return -1
# Test array
arr=[2, 3, 4, 10, 40 ]
x=4
# Function call
result=binarySearch(arr, 0, len(arr)-1, x)
ifresult!= -1:
print"Element is present at index% d"% result
else:
print"Element is not present in array"
Sample Output:
$pythonmain.py
Element is present at index 2
Program7: Write aPython Programtoperformselection sort.
Aim:
To write aPython Program to perform selection sort.
Algorithm:
1. Create afunction named selectionsort
2.Initialisepos=0
3.If alist[location]>alist[pos]then perform the followingtilli+1,
4. Set pos=location
5. Swap alist[i]and alist[pos]
6. Print thesorted list
Program:
defselectionSort(alist):
fori in range(len(alist)-1,0,-1):
pos=0
forlocation in range(1,i+1):
ifalist[location]>alist[pos]:
pos=location
temp =alist[i]
alist[i]=alist[pos]
alist[pos]=temp
alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)
SampleOutput:
$pythonmain.py
[17, 20, 26, 31, 44, 54, 55, 77, 93]
Program8: Write aPython Programtoperforminsertionsort.
Aim:
To write aPython Program to perform insertion sort.
Algorithm:
1. Create afunction named insertionsort
2.Initialisecurrentvalue=alist[index]and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the followingtilllen(alist)
4. alist[position]=alist[position-1]
5. position =position-1
6. alist[position]=currentvalue
7. Print thesorted list
Program:
definsertionSort(alist):
forindexin range(1,len(alist)):
currentvalue =alist[index]
position =index
while position>0 and alist[position-1]>currentvalue:
alist[position]=alist[position-1]
position =position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)
Sample Output:
$python main.py
[20, 54, 54, 54, 54, 54, 93, 93, 93]
Program9: Write aPython ProgramtoperformMerge sort.
Aim:
To write aPython Program to perform Mergesort.
Algorithm:
1. Create afunction named mergesort
2. Find themid of thelist
3. Assign lefthalf =alist[:mid]and righthalf=alist[mid:]
4.Initialise i=j=k=0
5. while i <len(lefthalf)and j <len(righthalf), perform the following if
lefthalf[i]<righthalf[j]:
alist[k]=lefthalf[i]
Increment i
else
alist[k]=righthalf[j]
Increment j
Increment k
6. while i <len(lefthalf),perform thefollowing
alist[k]=lefthalf[i]
Increment i
Increment k
7. while j <len(righthalf), perform thefollowing
alist[k]=righthalf[j]
Increment j
Increment k
8. Print thesorted list
Program:
# Python programforimplementation of MergeSort
# Merges two subarraysofarr[].
# First subarrayis arr[l..m]
# Second subarrayis arr[m+1..r]
def merge(arr, l, m, r):
n1 =m -l + 1
n2 = r-m
# createtemp arrays
L=[0]* (n1) R
=[0]* (n2)
mergeSort(arr,0,n-1)
print("\n\nSortedarrayis")
fori in range(n):
print("%d"%arr[i]),
Sample Output:
$python main.py
Given arrayis
12 11 13 5 6 7
Sorted arrayis
5 6 7 11 12 13
Experiment 10
Object: To write aPython program to find first n primenumbers
Algorithm:
SampleOutput:
$python main.py
Enter theupper limit:20
Primenumbers are
2
3
5
7
11
13
17
19
R program
Program 1: Write a R program to take input from the user (name and
age) and display the values. Also print the version of R installation.
Write a R program to get the details of the objects in memory.
R Programming Code
Sample Output:
R Programming Code
Sample Output:
Program 3: Write a R program to convert a given matrix to a 1
dimensional array.
R Programming Code
Sample Output:
Sample Output:
Program 5:Write a R program to create an empty data frame.
R Programming Code
Sample Output:
Program 6: Write a R program to create a data frame from four given
vectors.
R Programming Code
Sample Output:
Sample Output:
Program 12: Write a R program to create a vector of a specified type
and length. Create vector of numeric, complex, logical and character
types of length 6.
R Programming Code
Sample Output:
Program 13: Write a R program to add two vectors of integers type
and length 3.
R Programming Code
Sample Output:
Program 14: Write a R program to append value to a given empty
vector.
R Programming Code
Sample Output:
Sample Output:
Program 17: Write a R program to create a list containing a vector, a
matrix and a list and give names to the elements in the list. Access the
first and second element of the list.
R Programming Code
Sample Output:
[1] "List:"
[[1]]
[1] "Red" "Green" "Black"
[[2]]
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11
[[3]]
[[3]][[1]]
[1] "Python"
[[3]][[2]]
[1] "PHP"
[[3]][[3]]
[1] "Java"
$`Odd numbers`
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11
$`Language(s)`
$`Language(s)`[[1]]
[1] "Python"
$`Language(s)`[[2]]
[1] "PHP"
$`Language(s)`[[3]]
[1] "Java"
Sample Output:
Sample Output: