File 2
File 2
AIM
To write a program that accepts a number and checks whether the given number is Armstrong or not..
ALGORITHM
CODE
OUTPUT
AIM
To write a program that accepts a number and checks whether the given number is palindrome or not.
ALGORITHM
Page 1 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
STEP 1: Start the process
STEP 2 : Get the number (orig)
STEP 3 : Assign num=orig and rev=0
STEP 4 : Repeat steps 5, 6 and 7 while num>0 else goto step 8
STEP 5 : digit=num%10
STEP 6 : rev=rev*10+digit
STEP 7 : num=int(num/10)
STEP 8 : if orig== rev ,print “palindrome”,goto step 10
Otherwise goto step 9
STEP 9 : print “not a aplindrome”
STEP 10 : Stop the process
CODE
orig=int(input('enter a number'))
num=orig
rev=0
while num>0:
digit=num%10
rev=rev*10+digit
num=int(num/10)
if orig==rev:
print('palindrome')
else:
print('not a palindrome')
OUTPUT
enter a number875
not a palindrome
enter a number12321
palindrome
AIM
Write a program to find and display the sum of the given series till n terms, where n is defined by the user.
ALGORITHM
Page 2 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
FLOW CHART
START
import math
ACCEPT N
SUM = 0 , I = 1
NO
Is I <= n
YES
COMPUTE
SUM = SUM+1/(POW(2,I))
DISPLAY SUM
CODE : STOP
import math
sum=0
n=int(input(" Enter the number : " ))
for i in range(1,n+1):
print('1/',pow(2,i),'+',end='')
sum=sum+1/(pow(2,i))
print("0 \n The sum of the series is : ",sum)
OUTPUT
Page 3 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
Enter the number : 7
1/ 2 +1/ 4 +1/ 8 +1/ 16 +1/ 32 +1/ 64 +1/ 128 +0
The sum of the series is : 0.9921875
AIM
Write a program that accepts N and displays a numeric pattern for N terms
ALGORITHM
Page 4 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
STEP 7 : End the process
FLOW CHART
START
Accept N
I = J = 1
Is I <N+1 NO
YES
Is J < I NO
YES
Print(I,end=’’)
Print(’’)
STOP
CODE
for i in range(N+1):
for j in range(i):
print( i, end=' ')
print('')
OUTPUT
1
22
333
Page 5 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
4444
55555
666666
7777777
AIM
Write a program to count the lines, words, and characters in a user defined string.
ALGORITHM
CODE
mainstr=s=''
while True:
s=input(" Enter a line : ")
mainstr=mainstr+s+'\n'
ch=input("\n\t Press y to enter more lines other wise press N : ")
if ch in 'Nn':
break
char=word=line=0
for i in mainstr:
char=char + 1
if i=='\n':
line=line+1
for i in mainstr.split():
word=word+1
Page 6 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
print( '\n \t The total number of characters in the given string is ', char)
print( '\n \t The total number of words in the given string is ', word)
print( '\n \t The total number of lines in the given string is ', line)
OUTPUT
AIM
Write a program to count the number of vowels, consonents, and digits in a user defined multiline string.
ALGORITHM
Page 7 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
else:
dig=dig+1
else:
continue
STEP 6 : Display the original string, and the count of vowels, consonants and digits
STEP 7 : Stop the process
FLOW CHART
CODE
mainstr=s=''
while True:
s=input(" Enter a line : ")
mainstr=mainstr+s+'\n'
ch=input("\n\t Press y to enter more lines other wise press N : ")
if ch in 'Nn':
break
vow=con=dig=0
for i in mainstr:
if i.isalnum():
if i.isalpha():
if i in 'AEIOUaeiou':
vow=vow+1
else:
con=con+1
else:
dig=dig+1
else:
continue
print( '\n \t The total number of vowels in the given string is ', vow)
print( '\n \t The total number of consonents in the given string is ', con)
print( '\n \t The total number of digits in the given string is ', dig)
OUTPUT
Enter a line : Scholars estimate that the first successful expansion of the Homo sapiens range beyond Africa
and across the Arabian Peninsula occurred from as early as 80,000 years ago to as late as 40,000 years ago,
although there may have been prior unsuccessful emigrations. Some of their descendants extended the
human range ever further in each generation, spreading into each habitable land they encountered. One
human channel was along the warm and productive coastal lands of the Persian Gulf and northern Indian
Ocean. Eventually, various bands entered India between 75,000 years ago and 35,000 years ago
Page 8 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
Press y to enter more lines other wise press N : n
AIM
Write a program to input a sentence. Enter a word separately to find the frequency of the given word in the
sentence. Finally, display the frequency of the given word in the sentence. Finally, display the frequency of
the word.
ALGORITHM
FLOWCHART
CODE
mainstr=s=''
mainstr=input(" \n\t Enter a string : ")
s=input(" \n\t Enter a word to be searched : ")
freq=0
freq=mainstr.count(s)
Page 9 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
print( '\n \t The original string is : ', mainstr)
AIM
Write a program to enter a set of numbers in a list. Find and display the maximum and minimum numbers
among them.
ALGORITHM
CODE
d=0
l=[]
while True:
d=int(input(" \t Enter a value : "))
l.append(d)
ch=input("\t Press y to enter more values in the list otherwise press N : ")
if ch in 'Nn':
break
print(" \t The list of integers is as : \n\t",l)
max=l[0]
min=l[0]
size=len(l)
for i in range(0,size):
if(l[i]>max):
max=l[i]
if (l[i]<min):
min=l[i]
Page 10 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
print("\t The minimum value in the given list is : ", min)
print("\t The maximum value in the given list is : ", max)
OUTPUT
Enter a value : 45
Press y to enter more values in the list otherwise press N : y
Enter a value : 23
Press y to enter more values in the list otherwise press N : y
Enter a value : 56
Press y to enter more values in the list otherwise press N : y
Enter a value : 2
Press y to enter more values in the list otherwise press N : y
Enter a value : 78
Press y to enter more values in the list otherwise press N : y
Enter a value : 33
Press y to enter more values in the list otherwise press N : n
The list of integers is as :
[45, 23, 56, 2, 78, 33]
The minimum value in the given list is : 2
The maximum value in the given list is : 78
Page 11 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
AIM:
Write a python program to accept a set of names in a list. Enter a name from the user and search for it in the given
list of names. If found then display “Search is successful” otherwise, display “The name is not enlisted ”.
ALGORITHM:
CODE :
pos=0
l=[]
d=''
while True:
d=input(" \t Enter a name : ")
l.append(d)
ch=input("\t Press y to enter more values in the list otherwise press N : ")
if ch in 'Nn':
break
print(" \t The list of names are : \n\t",l)
for i in range(0,size):
if(l[i]==name):
flag=True
pos=i
break
Page 12 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
else:
continue
if flag==True:
print("\t Search is successful")
print( "\t The name %s is found in position %s " %(name,pos+1))
else:
print("\t Search is unsuccessful. No such name exists")
OUTPUT :
AIM
Write a python program that allows user to perform any of the list operations given below :
Create a list with user defined values
Insert an element at desired position
Page 13 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
Modify an existing element
Delete an element using value
Sort in ascending order
Sort in descending
Display the list
ALGORITHM
CODE
list=[]
l=[]
while True:
d=eval(input(" \t Enter a value : "))
l.append(d)
ch=input("\t Press y to enter more values in the list otherwise press N : ")
if ch in 'Nn':
break
print(" \t The list is : \n\t",l)
Page 14 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
ch=0
while ch!=7:
print('''\n\t 1. Insert an element at desired position
\n\t 2. Modify an existing element
\n\t 3. Delete an element using value
\n\t 4. Sort in ascending order
\n\t 5. Sort in descending
\n\t 6. Display the list
\n\t 7. Exit ''')
ch=int(input(" \n\t Enter your choice : "))
if ch==1:
a= eval(input("\t Enter an element :"))
pos = int(input("\t Enter the position at which to be entered :"))
l.insert(pos,a)
elif ch==2:
a= eval(input("\t Enter the new element :"))
pos = int(input("\t Enter the position at which to be changed :"))
l[pos]=a
elif ch==3:
a= eval(input("\t Enter the element to be deleted :"))
l.remove(a)
elif ch==4:
l.sort()
elif ch==5:
l.sort(reverse=True)
elif ch ==6:
print(" \t The list is : \n\t",l)
elif ch==7:
break
else:
print("\n\t Wrong option ! Enter again ..")
OUTPUT
Enter a value : 12
Press y to enter more values in the list otherwise press N : y
Enter a value : 'asd'
Press y to enter more values in the list otherwise press N : y
Enter a value : 56.54
Press y to enter more values in the list otherwise press N : y
Enter a value : 'ter'
Page 15 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
Press y to enter more values in the list otherwise press N : n
The list is :
[12, 'asd', 56.54, 'ter']
5. Sort in descending
7. Exit
5. Sort in descending
7. Exit
Page 16 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
5. Sort in descending
7. Exit
5. Sort in descending
7. Exit
5. Sort in descending
Page 17 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
6. Display the list
7. Exit
5. Sort in descending
7. Exit
5. Sort in descending
7. Exit
PROGRAM NO. : 18
Page 18 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
AIM
ALGORITHM
CODE
n = random.randint(1,6)
print('\n\t\t ',n,' ',end='')
time.sleep(.00001)
print("\n Your Number is :",n)
ans=input("\n Play More? (Y) :")
if ans.lower()!='y':
play='n'
break
OUTPUT
Page 19 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
Play More? (Y) :N
PROGRAM NO. : 19
AIM
ALGORITHM
CODE
import math
while True :
print("\n\t MENU FOR MATH MODULE FUNCTION ")
print('''\n\t 1.SQRT()
\n\t 2.POW()
\n\t 3.LOG()
\n\t 4.SIN()
\n\t 5.COS()
\n\t 6.TAN()
\n\t 7.FABS()
\n\t 8.CEIL()
\n\t 9.FLOOR()
\n\t 10.EXIT ''')
ch=int(input("\n\t Enter your choice : "))
if ch==1:
S=float(input("\t Enter a value"))
Page 20 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
print("The sqrt of ",S," is ",math.sqrt(S))
elif ch==2:
P=float(input("\t Enter a value"))
q=int(input(" \t Enter the power to which it is to be raised :"))
print("The POW of ",P," is ",math.pow(P,q))
elif ch==3:
S=float(input("\t Enter a value"))
print("The log of ",S," is ",math.log(S))
elif ch==4:
S=float(input("\t Enter a value"))
print("The Sin of ",S," is ",math.sin(S))
elif ch==5:
S=float(input("\t Enter a value"))
print("The Cos of ",S," is ",math.cos(S))
elif ch==6:
S=float(input("\t Enter a value"))
print("The TAN of ",S," is ",math.tan(S))
elif ch==7:
S=float(input("\t Enter a value"))
print("The FABS of ",S," is ",math.fabs(S))
elif ch==8:
S=float(input("\t Enter a value"))
print("The CEIL of ",S," is ",math.ceil(S))
elif ch==9:
S=float(input("\t Enter a value"))
print("The FLOOR of ",S," is ",math.floor(S))
elif ch==10:
break
else:
print(" Entered wrong code")
continue
OUTPUT
1.SQRT()
2.POW()
3.LOG()
4.SIN()
5.COS()
6.TAN()
7.FABS()
8.CEIL()
Page 21 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
9.FLOOR()
10.EXIT
Page 22 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
Entered wrong code
PROGRAM NO. : 20
AIM
Write a program to calculate the mean, median & mode of a given set of values
ALGORITHM
CODE
import statistics
while True :
print("\n\t MENU FOR MATH MODULE FUNCTION ")
print('''\n\t 1.MEAN()
\n\t 2.MEDIAN()
\n\t 3.MODE()
\n\t 4.EXIT ''')
ch=int(input("\n\t Enter your choice : "))
if ch==1:
S=eval(input("\t Enter a list of integer values : "))
print("\t The MEAN of the given list ",S," is ",statistics.mean(S))
elif ch==2:
S=eval(input("\t Enter a list of integer values : "))
print("\t The MEDIAN of the given list ",S," is ",statistics.median(S))
elif ch==3:
S=eval(input("\t Enter a list of integer values : "))
print("\t The MODE of the given list ",S," is ",statistics.mode(S))
elif ch==4:
break
else:
Page 23 of 24
CHENNAI PUBLIC SCHOOL
TH ROAD * SH 50 * THIRUMAZHISAI * CHENNAI – 600 124
RECORD PROGRAMS
print(" \t\t Entered wrong code")
continue
OUTPUT
1.MEAN()
2.MEDIAN()
3.MODE()
4.EXIT
Page 24 of 24