PAGE NO.
AREA AND CIRCUMFERENCE OF THE CIRCLE
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
PAGE NO.
COMPUTE THE GCD OF TWO NUMBERS
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
PAGE NO.
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
PAGE NO.
FIND THE SQUARE ROOT OF A NUMBER (NEWTONS METHOD)
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
PAGE NO.
PAGE NO.
EXPONENTIATION (POWER OF A NUMBER)
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
PAGE NO.
FIND THE MAXIMUM OF A LIST OF NUMBERS
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
PAGE NO.
LINEAR SEARCH
PROGRAM:
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
PAGE NO.
BINARY SEARCH
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
PAGE NO.
INSERTION SORT
PROGRAM:
def insertsort(sample):
print("intial sample:", sample)
for i in range(1, len(sample)):
print(sample)
j=i
while(j!=0 and sample[j] < sample[j-1]):
sample[j-1], sample[j] = sample[j], sample[j-1]
j-=1
print("sorted list:",sample)
sample1 = [12,300,-90,-100-1000,1,4]
insertsort(sample1)
OUTPUT ::
Initial list: 12,300,-90,-100-1000,1,4
Sorted list: -1000,-100.-90,1,4,12,300
PAGE NO.
SELECTION SORT
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]
PAGE NO.
PAGE NO.
MERGE SORT
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
PAGE NO.
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')
PAGE NO.
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
PAGE NO.
PAGE NO.
MULTIPLY MATRICES
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]
PAGE NO.
PROGRAMS THAT TAKE COMMAND LINE ARGUMENTS (WORD COUNT)
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
PAGE NO.
FIND THE MOST FREQUENT WORDS IN A TEXT READ FROM A FILE
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
PAGE NO.
SIMULATE ELLIPTICAL ORBITS IN PYGAME
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):
PAGE NO.
#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]()
PAGE NO.
OUTPUT ::
PAGE NO.
SIMULATE BOUNCING BALL USING PYGAME
PROGRAM:
import sys
import pygame
[Link]()
size = width, height = 320, 240
speed = [2, 2]
black = 0, 0, 0
screen = [Link].set_mode(size)
ball = [Link]('C:\\Users\\admin\\Desktop//[Link]')
ballrect = ball.get_rect()
while 1:
for event in [Link]():
if [Link] == [Link]: [Link]()
ballrect = [Link](speed)
if [Link] < 0 or [Link] > width:
speed[0] = -speed[0]
if [Link] < 0 or [Link] > height:
speed[1] = -speed[1]
[Link](black)
[Link](ball, ballrect)
[Link]()
PAGE NO.
OUTPUT :