Sie sind auf Seite 1von 17

PROGRAM TO COMPUTE THE GCD OF TWO NUMBERS

a=int(input("enter the value of a:"))


b=int(input("enter the value of b:"))
i=1
while(i<=a and i<=b):
if(a%i==0 and b%i==0):
gcd=i
i=i+1
print("gcd=",gcd)
lcm=(a*b)//gcd
print("lcm=",lcm)

output:
enter the value of a:5
enter the value of b:84
gcd= 1
lcm= 420
PROGRAM TO FIND THE SQUARE ROOT OF A NUMBER
(NEWTON’S METHOD)

PROGRAM

defnewtonsqrt(n):
root=n/2
for i in range(10):
root=(root+n/root)/2
print(root)
n=eval(input("enter number to find sqrt:"))
newtonsqrt(n)

output:

enter number to find sqrt:57


7.54983443527075
PROGRAM FOR FINDING EXPONENTIATION
( POWER OF A NUMBER )
PROGRAM

n=int(input("enter the positive number:"))


e=int(input("enter the exponent number:"))
power=1
for i in range(1,e+1):
power=power*n
print("res=",power)

output:
enter the positive number:27
enter the exponent number:18
res= 58149737003040059690390169
PROGRAM TO FIND THE MAXIMUM OF A LIST OF NUMBERS

PROGRAM

a=eval(input("enter the list"))

print(a)

max=a[0]

for i in a:

if i>max:

max=i

print("maximum is",max)

OUTPUT

enter the list[34,56,87,45,32]

[34, 56, 87, 45, 32]

maximum is 87
PROGRAM FOR LINEAR SEARCH
PROGRAM

a=eval(input("enter the list"))

print(a)

search=eval(input("enter search element"))

for i in a:

if search==i:

print("element is found")

break

else:

print("element is not found")

OUTPUT

>>>

enter the list[44,87,56,96,68]

[44, 87, 56, 96, 68]

enter search element96

element is found

>>>

enter the list[44,87,56,96,68]

[44, 87, 56, 96, 68]

enter search element69

element is not found


PROGRAM FOR BINARY SEARCH
PROGRAM

a=eval(input("enter list"))
a .sort ()
print ( "Sorted List = " , a )
search=eval(input("Enter Search Number"))
start = 0
stop =len(a) - 1
while ( start <= stop ) :
mid = ( start + stop ) // 2
if ( search == a[mid] ):
print( search , "element found at the position" , mid)
break
elif ( search < a[mid] ):
stop = mid - 1
else:
start = mid + 1
else:
print ( "element not found" )

OUTPUT
enter list [2,5,-1,98,-100,14,15]
Sorted List = [-100, -1, 2, 5, 14, 15, 98]
Enter Search Number5
5 element found at the position 3
PROGRAM FORSELECTION SORT

PROGRAM

a = [ 16,10,-1,369,5 ]

for i in range( len (a) ):

small = min ( a[ i: ] )

index = a.index ( small )

a[ i ] , a [ index ] = a [ index ] , a[ i ]

print ( a )

OUTPUT

[-1, 5, 10, 16, 369]


PROGRAM FORINSERTION SORT

PROGRAM

a = [ 16,10,-1,369,5 ]

for i in a :

j = a.index( i )

while j > 0 :

if a[ j -1 ] > a[j] :

a[j-1],a[j] = a[j],a[j-1]

else:

break

j=j-1

print ( a )

OUTPUT

[-1, 5, 10, 16, 369]


PROGRAM FOR MERGE SORT

def merge(a,b):
c = []
whilelen(a) != 0 and len(b) != 0:
if a[0] < b[0]:
c.append(a[0])
a.remove(a[0])
else:
c.append(b[0])
b.remove(b[0])
iflen(a) == 0:
c=c+b
else:
c=c+a
return c

def divide(x):
iflen(x) == 0 or len(x) == 1:
return x
else:
middle = len(x)//2
a = divide(x[:middle])
b = divide(x[middle:])
return merge(a,b)

x=[5,2,9,3,1,8]
c=divide(x)
print(c)

OUTPUT
Sorted Number for Merge Sort

[1,2,3,5,8,9]
PROGRAM TO FIND THE FIRST ‘N TERMS’ PRIME NUMBERS
PROGRAM

n=eval(input("enter n :"))

count=1

num=2

while count<=n:

for i in range (2,num,1):

ifnum%i==0:

break

else:

print(num)

count=count+1

num=num+1

OUTPUT

>>>

enter n : 5

11
PROGRAM FOR MATRIX MULTIPLICATION

print( " Matrix Multiplication " )

a=[ [ 2 , 2 ] ,[ 2 , 2 ] ]

b=[ [ 3 , 3 ] ,[ 3 , 3 ] ]

c=[ [ 0 , 0 ] ,[ 0 , 0 ] ]

for i in range ( len ( a ) ):

for j in range ( len ( b) ):

for k in range ( len ( b ) ):

c [ i ] [ j ] = a [ i ][ k ] * b [ k ][ j ] + c [ i ] [ j ]

fori in c :

print(i)

OUTPUT

Matrix Multiplication

[ 12 , 12 ]

[ 12 , 12 ]
PROGRAM FOR COMMAND LINE ARGUMENTS ( WORD COUNT )

from sys import argv


from collections import Counter
f = open(argv[1] ," r ")
c = Counter ( f.read( ) . split( ) )
print( " Number of words in the file : " , c )

INPUT (ex.txt)

panimalarannapanimalarannapanimalarannauniversity

OUTPUT

sam@syse:~$ python cmd.py ex.txt

('Number of words in the file :', Counter({'panimalar': 3, 'anna': 2,


'annauniversity': 1}))
PROGRAM TO FIND MOST FREQUENT WORDS IN A
TEXT READ FROM A FILE

from collections import Counter

f=open( "ex.txt" , "r" )

c=Counter(f.read( ) . split( ) )

print("Number of words in the file :",c)

INPUT (ex.txt )

panimalarannapanimalarannapanimalarannauniversity

OUTPUT

sam@syse:~$ python coun.py

('Number of words in the file :', Counter({'panimalar': 3, 'anna': 2,


'annauniversity': 1}))
SIMULATE ELLIPTICAL ORBITS IN PYGAME
importpygame
import math
from time import *
pygame.init()
screen = pygame.display.set_mode((700, 700))
black = (0, 0, 0)
yellow = (255, 255, 0)
blue = (0, 0, 255)
ex = 50
ey = 350
oe = 0
a=1
while a==1:
screen.fill(black)

ex = math.cos(oe) * 300 + 350


ey = -math.sin(oe) * 300 + 350

oe += .002

pygame.draw.circle(screen, yellow, (350,350), 50)


pygame.draw.circle(screen, blue, (int(ex), int(ey)), 15)

pygame.display.flip()
sleep(.010)

for event in pygame.event.get():


ifevent.type == pygame.QUIT:
a=0

pygame.quit()
OUTPUT
SIMULATE BOUNCING BALL USING PYGAME
importpygame
import math
from time import *

pygame.init()
screen = pygame.display.set_mode((700, 700))

white = (255, 255, 255)


yellow = (255, 255, 0)
blue = (0, 0, 255)

ex = 50
ey = 350
oe = 0

om = 0

a=1

while a==1:
screen.fill(white)

ex = math.cos(oe) * 300 + 350


ey = -math.sin(om) * 300 + 350

oe += 0.002

om += 0.01

pygame.draw.circle(screen, blue, (int(ex), int(ey)), 15)

pygame.display.flip()
sleep(.010)

for event in pygame.event.get():


if event.type == pygame.QUIT:
a=0
pygame.quit()
OUTPUT

Das könnte Ihnen auch gefallen