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

Ex - No:1 Date:: 1. GCD of Two Numbers

The document contains algorithms for 13 Python programs, including programs to find the GCD and square root of numbers, perform exponentiation, find the maximum of a list, conduct linear and binary searches, implement selection, insertion, and merge sorts, find prime numbers, multiply matrices, use command line arguments, find frequent words in a text file, simulate elliptical orbits in Pygame, and simulate a bouncing ball in Pygame.

Uploaded by

vanitha
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Ex - No:1 Date:: 1. GCD of Two Numbers

The document contains algorithms for 13 Python programs, including programs to find the GCD and square root of numbers, perform exponentiation, find the maximum of a list, conduct linear and binary searches, implement selection, insertion, and merge sorts, find prime numbers, multiply matrices, use command line arguments, find frequent words in a text file, simulate elliptical orbits in Pygame, and simulate a bouncing ball in Pygame.

Uploaded by

vanitha
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Ex.

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:

1. Create a function named insertionsort


2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]
5. position = position-1
6. alist[position]=currentvalue
7. Print the sorted list

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

8 .FIRST N PRIME NUMBERS


Aim:
To write a Python program to find first n prime numbers.
3
Algorithm:
1. Read the value of n
2. for num in range(0,n + 1), perform the following
3. if num%i is 0 then break
else print the value of num
4. Repeat step 3 for i in range(2,num)

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:

Thus the Python Program to simulate elliptical orbits using Pygame is


executed successfully and the output is verified.

13. SIMULATE BOUNCING BALL


Aim:
To write a Python program to simulate Bouncing Ball in Pygame.

Algorithm:

1. Import the required packages


2. Define the required variables
3. Define the screen space to display the bouncing balls in that space

Result:

Thus the Python Program to simulate bouncing ball using Pygame is executed
successfully and the output is verified.

You might also like