[Go to site: main page, start]

0% found this document useful (0 votes)
11 views20 pages

Python Programs for Basic Algorithms

The document contains various Python programs that demonstrate different functionalities such as computing GCD, checking for prime numbers, identifying leap years, and finding Armstrong numbers. It also includes string operations, character counting, list manipulation, searching algorithms, sorting algorithms, and file handling. Each program is presented with input prompts and expected outputs, showcasing basic programming concepts and operations.

Uploaded by

shanthis57525
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views20 pages

Python Programs for Basic Algorithms

The document contains various Python programs that demonstrate different functionalities such as computing GCD, checking for prime numbers, identifying leap years, and finding Armstrong numbers. It also includes string operations, character counting, list manipulation, searching algorithms, sorting algorithms, and file handling. Each program is presented with input prompts and expected outputs, showcasing basic programming concepts and operations.

Uploaded by

shanthis57525
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

TO COMPUTE GCD OF TWO NUMBERS

PROGRAM:

a= int (input(‘Enter the value of a: ’))


b= int (input(‘Enter the value of b: ’))
a1=a
b1=b
while b!=0:
t=b
b=a%b
a=t
print (“the GCD of { } and { } is { }”.format(a1,b1,a))
TO PRINT PRIME NUMBERS IN THE GIVEN RANGE

PROGRAM:
n1= int (input(‘Enter the first number in the range: ’))
n2= int (input(‘Enter the second number in the range: ’))
for i in range (n1,n2+1):
if i > 2:
for j in range(2,i):
r=i % j
if r = = 0:
break
if r!=0:
print(i,end=”)
else:
print(i,end=”)
TO CHECK THE GIVEN YEAR IS LEAP YEAR OR NOT

PROGRAM:
year = int(input(‘ enter a year: ‘))
if ( (year % 4 = = 0 and year % 100 ! = 0) or year % 400 = = 0):
print ( ‘ the given year{}is leap’.format(year))
else:
print(‘the given year { } is not leap’. Format(year))
TO PRINT ARMSTRONG NUMBERS BETWEEN GIVEN RANGE

PROGRAM
Import math
n1= int (input(‘Enter the first number in the range: ’))
n2= int (input(‘Enter the second number in the range: ’))
for i in range (n1,n2+1):
power = int (len(str(i))) # To find the number of digits in the numer
sum = 0
t=i
while t!=0:
d=t % 10 # To separate individual digit
sum =sum + [Link]( d,power)
t=t/10
if sum=i:
print( i)
TO DO BASIC TRIM AND SLICE OPERATION ON STRING

PROGRAM:
s=’welcome to python’
print(‘The sliced string from index 2 to 8 is: ‘,s[2:9])
print(‘ The sliced string from index 0 to 5 is: ‘s[:6])
print(‘ The sliced string from index 5 to last is:’,s[5:])
s1=’ Good Morning’
print(‘ the string after removing leading and trailing white spaces is : s1,strip( ))
print(‘ the string after removing leading white spaces is : s1,lstrip( ))
print(‘ the string after removing leading white spaces is : s1,rstrip( ))
TO FIND THE NUMBERS OF CHARACTERS, VOWELS AND BLANK

Program:
S=input(“Enter a string: “)
Vowels=0
characters=0
spaces=0
s=[Link]( )
vow=’aeiou’
for i in s:
if i in vow:
vowels = vowels +1
if(i>=’a’ and i<=’z’):
characters=characters+1
else:
spaces=spaces+1
print(‘vowels:’,vowels);
print(‘characters:’, characters);
print(‘spaces:’, spaces);
TO DISPLAY ALL SUCH NUMBERS WHICH IS DIVISIBLE BY 3 BUT ARE NOT
MULTIPLE OF 5 IN A GIVEN RANGE

PROGRAM:
n1= int (input(‘Enter the first number in the range: ’))
n2= int (input(‘Enter the second number in the range: ’))
print(‘The numbers which are divisible by 3 but not multiple of 5 are:’)
for i in range (n1,n2+1):
if i%5!=0 and i%3==0:
print(i,’\t’,end=”)
FIBONACCI SERIES USING RECURSION
PROGRAM:
Def fib(n):
If n= = 1:
return 0
elif n= = 2:
return 1
else:
return(fib(n-1)+fib(n-2))
#main program
n = int(input(‘Enter the value of n: ‘))
if n<=0:
print(‘Enter a positive integer’)
print(‘ The Fibonacci sequence for {}is :’.format(n))
for i in range(1,n+1):
print(fib(i),end=’ ‘)
STRING OPERATION
PROGRAM:
s= input(‘enter a string: ‘)
s1=s
l=len(s)
if l > 3:
if s[-3:] = =’ing’:
s= s+’ly’
else:
s=s+’ing’
print(‘the given string is:’,s1)
print(‘The modified string is: ‘ ,s)
TO FIND MINIMUM AND MAXIMUM OF A LIST OF NUMBER

PROGRAM
L= [ ]
N= int(input(‘Enter the total number:’))
Print(‘Enter the number one by one’)
For i in range(N):
X=int(input( ))
[Link](x)
min1=min(L)
max1=max(L)
posmin=[Link](min1)
posmax=[Link](max1)
print(‘The minimum number among the given numbers is : ‘ , min1)
print(‘ The position of the minimum number is:’,posmin)
print(‘The maximum number among the given numbers is : ‘ , max1)
print(‘The position of the maximum number is :’,posmax)
TO DISPLAY A LIST IN REVERSE ORDER

PROGRAM
L= [ ]
N= int(input(“Enter the total number of the list elements: ”))
Print(‘Enter the elements one by one: ’)
For i in range(N):
x = int(input ( ) )
[Link](x)
print(‘ The original list is : ‘ , L)
print(‘ the reversed list is : ‘ , end=”)
[Link]( )
print(L)
TO DISPLAY A LIST IN REVERSE ORDER

PROGRAM
L= [ ]
N= int(input(‘Enter the total number of list elements:”))
Print(‘Enter the elements one by one’)
For i in range(N):
x=int(input( ))
[Link](x)
Print(‘The Orginal list is : ‘ ,L)
Print(‘The reversed list is : ‘end=”)
For i in range(N-1,-1,-1):
Print(L[i],’\t’,end= “)
TO FIND AND PRINT A TUPLE

PROGRAM
t=(3,4,5,6,7,19,70,’good’,’bad’,89,76)
l=len(t)
print(‘The given tuple is: ‘ , t)
print(‘First half tuple values are:’,t[:1/2])
print(‘First second tuple values are:’,t[:1/2:])
TO FIND LONGEST WORD IN THE LIST

PROGRAM
L= [ ]
N= int(input(‘Enter the total number of words elements:”))
Print(‘Enter the words one by one’)
For i in range(N):
w=input( )
[Link](x)
x=l[0]
l1=len(x)
for i in l:
l2=len(i)
if l2>l1:
l1=l2
x=i
print(“The longest word in the list { } is { } “.format(l,x))
LINEAR SEARCH

PROGRAM
N= int(input(‘Enter the total number of data to sort:”))
a=[ ]
For i in range(N):
x== int(input(‘Enter the next data:’))
[Link](x)
x= int(input(‘Enter the element to search:’))
For i in range(N):
If a[i] = = x:
Print(“the element { } is present in position { }”.format(x,i))
Break
If i ==n-1:
Print(“search failed”)
SELECTION SORT

PROGRAM
N= int(input(‘Enter the total number of data to sort:”))
a=[ ]
For i in range(N):
x== int(input(‘Enter the next data:’))
[Link](x)
print(‘The Original list is:’,a))
For i in range(N):
Min = a[i]
t=i
for j in range(i+1,n):
if min > a[j]:
min =a[j]
t=j # gives index position of the next smallest value
a[t] =a[i]
a[i]=min
print(‘the sorted list is :’,a)
MULTIPLY TWO MATRIX

PROGRAM
a=[[1,2,3],[4,5,6]]
b=[[1,2,3],[4,5,6],[1,3,3]]
c=[[0,0,0],[0,0,0]]
m=len(a)
n=len(b)
for i in range(m):
for j in range(len(b)):
c[i][j]+=a[i][k]*b[k][j]
print(‘The product of the two given matrix is’)
for i in range(m):
for j in range(len(b)):
print(c[i][j],end=’ ’)
print ( )
TO DEMONSTRATE DIFFERENT OPERATION ON TUPLE

PROGRAM
t1=( 17,’bat’97.5,’cat’,’dog’)
t2=(89.7,45,76)
t3=t1+t2 #concatenation
t4=t3*2 # repetition
l=len(t3) # finding the length of the tuple t3
print(‘The concatenated tuple is:’ ,t3)
print(‘The repeated tuple is:’ ,t4)
print(‘The length of the tuple t3 is:’ ,l)
print(‘The minimum and maximum elements in tuple t2 are:’ ,max(t2),min(t2))
TO DEMONSTRATE TO USE DICTIONARY AND RELATED FUNCTIONS

PROGRAM
d={1001:’ramu’,1001:’Babu’,1003:’kumar’,1004:’somu’1005:’vijayshree’}
print(‘The length of the dictionary is :’len(d))
print(‘The copied dictionary is :’[Link]( ))
print(‘The removed element is :’[Link](1003))
print(the dictionary d after removing 1003 is : ’ ,d)
print(the element in1002key is : ’ ,[Link](1002))
print(the key values pair as a tuple in a list is : ’ ,[Link]( ))
print(the dictionary values as a list is: ’ ,[Link]( ))
print(the removed last item from dictionary is : ’ ,[Link]( ))
[Link]( )
print(the removed after removing all elements is : ’ ,d)
TO COPY FILE CONTENTS FROM ONE FILE TO ANOTHER
AND DISPLAY NUMBER OF WORDS COPIED.

PROGRAM
with open(‘[Link]’,’w’)as f:
n=int(input(‘enter the number of lines of data:’)
[Link](data + ‘\n’)
with open open(‘[Link]’,’r’)as f:
with open open(‘[Link]’,’w’)as s:
for i in f:
[Link](i)
with open open(‘[Link]’,’r’)as s:
print(‘the contentof file2 is:\n’,[Link]( ))
with open open(‘[Link]’,’r’)as f:
d=[Link]( )
l=[Link]( )
count=0
for i in l:
count=count + 1
print(‘Number of words copied from file1 to file2 is :’ , count)

You might also like