AREA AND CIRCUMFERENCE OF THE CIRCLE
Aim:
To write a program to find the area and circumference of the circle.
Algorithm:
Step 1: start
Step 2: input the radius of the circle
Step 3: find the area and circumference of the circle using the formulae
Area=3.14*r*r
Circum=2*3.14*r
Step 4: print the area and the circum of the circle.
Step 5: stop.
Flow Chart :
start
Read Radius
Area=3.14*r*r
Circum=2*3.14*r
Print area,circum
stop
Program
r=int(input("enter the radius of the circle"))
area=3.14*r*r
circum = 2*3.14*r
print('area:',area)
print('circumference:',circum)
Output
enter the radius of the circle 5
area: 78.5
circumference: 31.40000000
COMPUTE THE GCD OF TWO NUMBERS
Aim:
To write a program to compute the GCD of two numbers.
Algorithm:
Step 1: start
Step 2: Read 2 numbers to find the GCD,n1,n2
Step 3: rem = n1 % n2
Step 4: WHILE rem !=0
Step 4.1: n1=n2
Step 4.1: n2=rem
Step 4.1: rem = n1 % n2
Step 5: Print GCD is n2
Step 6: stop.
Program
n1=int(input("enter a number"))
n2=int(input("enter an another number"))
rem=n1%n2
while rem!=0:
n1=n2
n2=rem
rem=n1%n2
print("Gcd of two numbers is:",n2)
Output:
enter a number 54
enter an another number 24
Gcd of two numbers is: 6
Algorithm:
Step 1: start
Step 2: import the fractions module
Step 3: take in both the integer and store it in separate variables
Step 4: use the in-built functions to find the GCD of both the numbers.
Step 5: Print the GCD
Step 6: stop.
Program
import fractions
a=int(input("enter the first number"))
b=int(input("enter the second number"))
print("The GCDof two [Link]",[Link](a,b))
Output:
enter the first number 15
enter the second number 15
The GCDof two [Link] 5
Result:
Thus the Python program to compute GCD of two numbers is executed successfully and the output is
verified.
Find the square root of a number (Newtons method)
Aim:
To find the squareroot of a number using python.
Algorithm:
Step1 Start
Step2 Read the input from n,a
Step3 Approx = 0.5*n
Step4 For i upto range a
Betterapprox = 0.5*(approx+n/approx)
Approx = betterapprox
Return betterapprox
Call the function and print newtonsqrt(n,a)
Stop
Program:
def newtonSqrt(n, a):
approx = 0.5 * n
for i in range(a):
betterapprox = 0.5 * (approx +n/approx)
approx = betterapprox
return betterapprox
print(newtonSqrt(10, 3))
print(newtonSqrt(10, 5))
print(newtonSqrt(10, 10))
Ouput:
3.162319422150883
3.162277660168379
3.162277660168379
Exponentiation (power of a number)
Aim
To write a program to find exponentiation of a number (power of a number).
Algorithm
Step1: Start
Step 2: r n
Step 3:Read Base Number n
Step 4: For Exponent Number e
Step 5:For i1 to e
Step 5.1: Compute rn* r
Step 6: Print ‘Exponent ’r
Step 7:Stop.
Program
n=int(input("enter no."))
e=int(input("enter exponent"))
r=n
for i in range (1,e):
r=n*r
print('exponentiation is:',r)
Output
enter no.3
enter exponent3
exponentiation is: 27
Find the maximum of a list of numbers
Aim:
To find the maximum of a list of numbers using python.
Algorithm: observation note
Program
lst=[ ]
num=int(input('how many numbers'))
for n in range(num):
numbers = int(input('enter numbers'))
[Link](numbers)
print("maximum element in the list is:",max(lst),"\n minimum element in the list is:",min(lst))
Output
how many numbers 5
enter numbers 8
enter numbers 10
enter numbers 12
enter numbers 1
enter numbers 17
maximum element in the list is: 17
minimum element in the list is: 1
how many numbers 4
enter numbers 1
enter numbers -1
enter numbers 5
enter numbers 10
maximum element in the list is: 10
minimum element in the list is: -1
Linear Search
Aim
To search an element using linear or sequential search.
Algorithm
Step1:start
Step2: Read n elements into the list
Step3: Read the element to be searched
Step4: If alist[pos]==item, then print the position of the item
Step5: Else increment the position and repeat step 3 until pos reaches the length of the list.
Programs:
def search(alist,item):
pos=0
found=False
stop=False
while pos<len(alist) and not found and not stop:
if alist[pos]==item:
found=True
print("element found in position",pos)
else:
if alist[pos]>item:
stop=True
else:
pos=pos+1
return found
a=[]
n=int(input("enter upper limit"))
for i in range(0,n):
e=int(input("enter the elements"))
[Link](e)
x=int(input("enter element to search"))
y=int(input("enter element to search"))
search(a,x)
search(a,y)
output:
enter upper limit 4
enter the elements 12
enter the elements 23
enter the elements 34
enter the elements 45
enter element to search 45
enter element to search 12
element found in position 3
element found in position 0
binary search
Aim:
To write a Python Program to perform binary search.
Algorithm:
Step 1. Read the search element
Step 2. Find the middle element in the sorted list
Step 3. Compare the search element with the middle element
[Link] both are matching, print element found
[Link] then check if the search element is smaller or larger than the middle element
Step 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
Step 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
Step 6. Repeat the process until the search element if found in the list
Step 7. If element is not found, loop terminates
Program:
def bsearch(alist,item):
first=0
last=len(alist)-1
found=False
while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:
found=True
print("element found in position",mid)
else:
if item<alist[mid]:
last=mid-1
else:
first=mid+mid-1
return found
a=[]
n=int(input("enter upper limit"))
for i in range(0,n):
e=int(input("enter the elements"))
[Link](e)
x=int(input("enter element to search"))
bsearch(a,x)
Output
enter upper limit 3
enter the elements 12
enter the elements 23
enter the elements 34
enter element to search 12
element found in position 0
INSERTION SORT
Program:
def isort(a):
for index in range(1,len(a)):
currentvalue = a[index]
position = index
while position > 0 and a[position-1]>currentvalue:
a[position]=a[position-1]
position = position-1
a[position]=currentvalue
a=[]
n=int(input('enter upper limit'))
for i in range(n):
[Link](int(input()))
isort(a)
print('list after sort:',a)
SELECTION SORT
Aim: To sort list of elements using selection sort.
Algorithm:
Start
Read upper Limit n
Read n elements to the list
For I = I to len(sample)
o while(j <len(sample))
o Repeat the steps until condition satisfied
Call function Selsort() print the sorted elements.
Program:
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)
output:
[1,10,13,35,45,80,100,147,500]
Merge sort
Aim:
To sort list of elements using merge sort.
Algorithm
Start
Divide the arrays in left sub array & right sub array
Conquer by recursively sorting the two sub arrays
Combine the elements back in by merging the two sorted sub arrays
Call the results and print the arrays
Stop
Program:
a=[]
c=[]
n1=int(input("enter the no of element"))
for i in range(1,n1+1):
b=int(input("enter element"))
[Link](b)
n2=int(input("enter no of element"))
for i in range(1,n2+1):
d=int(input("enter element"))
[Link](d)
new=a+c
[Link]()
print("sorted list is",new)
output:
enter the no of element
3
13
11
4
Enter the no of element
2
14
2
Sorted list is
2 , 4 , 11, 13, 14
First n prime numbers.
Program:
n1=int(input("enter the upper limit of element"))
for num in range(1,n1):
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 of element 24
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 is a prime number
9 equals 3 * 3
10 equals 2 * 5
11 is a prime number
12 equals 2 * 6
13 is a prime number
14 equals 2 * 7
15 is a prime number
15 equals 3 * 5
16 equals 2 * 8
17 is a prime number
18 equals 2 * 9
19 is a prime number
20 equals 2 * 10
21 is a prime number
21 equals 3 * 7
22 equals 2 * 11
23 is a prime number
Multiply Matrices
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)):
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
Program
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(Y[0])):
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
for r in result:
print(r)
Output:
[114, 160, 60]
[74, 97, 73]
[119, 157, 112]
Programs that take command line arguments(word count)
Aim:
To find the word and lines in command line arguments.
Algorithm:
Start
Add arguments to find the words and lines
Add file name as argument
Parse the arguments to get the values
Format and print the words
Stop
Program:
fname = input("Enter file name: ")
num_words = 0
with open(fname, 'r') as f:
for line in f:
words = [Link]()
num_words += len(words)
print("Number of words:")
print(num_words)
Output:
Enter file name: [Link]
Number of words: 27
Find the most frequent words in a text read from a file.
Aim:
To find the most frequent words in a text read from a file.
Algorithm:
Start
Read the filename
Open the file
Read each line from the file to count the lowers and words
Split each line in to words and count them
Print the word and counts
Stop
Program:
fr = open("[Link]","r")
wordcount = {}
for word in [Link]().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
for k,v in [Link]():
print(k, v)
[Link]()
Output:
To - 1
Find - 1
The - 1
Most-1
Frequent-1
Words-1
In-1
A-2
Text-1
Read-1
From-1
File-1
Exercise 12 SIMULATE ELLIPTICAL 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
Program:
import math
import random
import pygame
class Particle ():
def __init__ (self, x, y, colour=0x000000):
self.x = x
self.y = y
[Link] = 0
[Link] = 0
[Link] = colour
def apply_gravity (self, target):
dsqd = (self.x - target.x) ** 2 + (self.y - target.y) ** 2
#g = G*m/dsqd * normalized (self - target)
if dsqd == 0:
return
[Link] += -1 / dsqd * (self.x - target.x) / dsqd ** 0.5
[Link] += -1 / dsqd * (self.y - target.y) / dsqd ** 0.5
def update (self):
self.x += [Link]
self.y += [Link]
[Link]()
window = [Link].set_mode((600, 400))
main_surface = [Link] ((600, 400))
colours = [0x000000, 0x111111, 0x222222, 0x333333, 0x444444, 0x555555, 0x666666,
0x777777, 0x888888, 0x999999, 0xaaaaaa, 0xbbbbbb] + [0xFF0000,
0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888,
0xFFFFFF, 0x808000, 0x008080, 0x800080, 0x800000]
#colours = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888,FFFFF,
0x808000, 0x008080, 0x800080, 0x800000]
particles = [Particle (200, 100, colours [i]) for i in range (20)]
earth = Particle (200, 200)
for i, p in enumerate (particles):
[Link] = i / 100
while (True):
# main_surface.fill(0x000000)
[Link] (main_surface, 0x00FF00, (earth.x, earth.y), 5, 2)
for p in particles:
p.apply_gravity (earth)
[Link] ()
[Link] (main_surface, [Link], (int (p.x), int (p.y)), 5, 2)
[Link](main_surface, (0, 0))
[Link]()
output:
Result:
Thus the Python Program to simulate elliptical orbits using Pygame is executed
successfully and the output is verified.