Ex - No:1 Date:: 1. GCD of Two Numbers
Ex - No:1 Date:: 1. GCD of Two Numbers
No:1
Date:
1. GCD OF TWO NUMBERS
Aim:
To write a Python program to find 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
Result:Thus the Python program to compute GCD of two numbers is executed successfully and the output
is verified.
2. Square root of a number by Newton’s Method.
Aim:
To write a Python Program to find the square root of a number by Newton’s Method.
Algorithm:
1. Define a function named newtonSqrt().
2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx
Result:
Thus the Python program for finding the square root of a given number by Newton’s Method is
executed successfully and the output is verified.
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.
04. FINDING MAXIMUM FROM A LIST OF NUMBERS
Aim:
To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l
2. Read the value of n
1
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
Result:
Thus a Python Program to find the maximum from the given list of numbers is successfully
executed and the output is verified.
.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
Result:
Thus the Python Program to perform Linear search is executed successfully and the output is verified
.5b BINARY SEARCH
AIM: To write a python program to perform the binary search.
ALGORITHM:
1.Read the search element
2.Find the middle element in the sorted list
3.Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4.If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element
5.If the search element is larger than the middle element, then repeat steps 2 and 3 for
the right sublist of the middle element
6.Repeat the process until the search element if found in the list
7.If element is not found, loop terminates
Result:
Thus the Python Program to perform binary search is executed successfully and the output is verified
6a.SELECTION SORT
Aim:
To write a Python Program to perform selection sort.
Algorithm:
1. Create a function named selectionsort
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
2
Result:Thus the Python Program to perform selection sort is successfully executed and the output is
verified.
6b.INSERTION SORT
Aim:
To write a Python Program to perform insertion sort.
Algorithm:
Result:
Thus the Python program to perform insertion sort is successfully executed and the output
is verified.
7 MERGE SORT
Aim:
To write a Python Program to perform Merge sort.
Algorithm:
1. Create a function named mergesort
2. Find the mid of the list
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 the following
alist[k]=lefthalf[i]
7. while j < len(righthalf), perform the following
alist[k]=righthalf[j]
Increment j
Increment k
8. Print the sorted list
Result:
Thus the Python program to perform merge sort is successfully executed and the output is
verified
Result:
Thus the Python Program to find the first n prime numbers is executed successfully and
the output is verified.
9 . MATRIX MULTIPLICATION
Aim:
To write a Python program to multiply matrices.
Algorithm:
1. Define two matrices X and Y
2. Create a resultant matrix named ‘result’
3. for i in range(len(X)):
i. for j in range(len(Y[0])):
a) for k in range(len(Y))
b) result[i][j] += X[i][k] * Y[k][j]
4. for r in result, print the value of r
Result:
Thus the Python program to multiply matrices is executed successfully and the output is verified.
10. COMMAND LINE ARGUMENTS (WORD COUNT)
Aim:To write a Python program for command line arguments.
Algorithm:
1. Add arguments to find the words and lines
2. Add the filename as argument
3. Parse the arguments to get the values
4. Format and print the words and lines
Result:
Thus the Python program for command line arguments is executed successfully and the output
is verified.
11.FIND THE MOST FREQUENT WORDS IN A TEXT READ FROM A FILE
Aim:
To write a Python program to find the most frequent words in a text read from a file.
Algorithm:
1. Read the filename
2. Open the file in read mode
3. Read each line from the file to count the lowers and words
4. Read each line from the file to replace the punctuations
5. Split each line into words and count them
6. Print the words and counts
Result:
Thus the Python program to find the most frequent words in a text read from a file is executed
successfully and the output is verified.
4
12. SIMULATE ELIPTICAL ORBITS IN PYGAME
Aim:
To write a Python program to simulate elliptical orbits in Pygame.
Algorithm:
1. Import the required packages
2. Set up the colours for the elliptical orbits
3. Define the parameters to simulate elliptical orbits
4. Display the created orbits
Result:
Algorithm:
Result:
Thus the Python Program to simulate bouncing ball using Pygame is executed
successfully and the output is verified.