Computer Practical File
Computer Practical File
Done By,
Elton s dsouza
XI B
Index
Grade-11 Programs
1
Write a menu driven program to print the sum, difference, product and quotient and remainder
of 2 numbers
Write a menu driven program to print the natural numbers, even numbers and odd numbers in
the given range
Write a menu driven program to print the sum of natural numbers, even numbers and odd
numbers in the given range
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
* *
* * *
* *
*
Write a program to print the following pattern
1
121
12321
1234321
123454321
Program to print the sum of following series:
1+5+9+13+....+n terms
Program to print the sum of following series:
1+x+x2+x3+....+n terms
Program to print the sum of following series:
(1/3)+(2/5)+(3/7)+....+n terms
Program to print the sum of following series:
(1/1!)+(2/2!)+(3/3!)+....+n terms
Program to print the sum of following series:
1+(1+2) +(1+2+3) +(1+2+3+4) +.
WAP to input a string and do the following:-
27
28
29
WAP to input a string and convert to title case using built in function.
30
31
Write a program to enter n data in a list and print highest and lowest value.
32
Write a program to enter n data in a list and print the sum and average of the values.
33
Write a program to exchange the first half of the list with the next half.
34
Write a program to enter n data in a list and exchange the values in adjacent places.
a. using temporary variable
b. without using temporary variable
35
Write a menu driven program to create a list and edit, append, delete and display elements
36
Write a program to print elements above and below the main and off diagonals.
37
Write a program to enter a matrix and print border and middle elements.
38
Write a menu driven program to create a matrix and display the following:
1)Display main diagonal
2)Display off diagonal
3)Display main diagonal using single loop
4)Display off diagonal using single loop
39
40
41
Write a program to print the sum of elements above and below main and off diagonals.
42
43
44
WAP to enter name and marks of students in the form of a dictionary. Also search a students
mark.
WAP to enter name and phone number in a dictionary.
45
46
WAP to enter name and marks of students in the form of a dictionary. Also search a students
mark.
Answers
# 1 - menu-driven pgm to print sum, difference, product, quotient and remainder of
2 nos.
def main():
print "\n\n\t\t\tMENU - OPTIONS"
print "\n\t\t1 --> Sum"
print "\t\t2 --> Difference"
print "\t\t3 --> Product"
print "\t\t4 --> Quotient"
print "\t\t5 --> Remainder upon Dividing"
print "\t\t6 --> Exit"
if int(choice)==1:
print "\n\tSum =",n1+n2
elif int(choice)==2:
print "\n\tDifference =",n1-n2
elif int(choice)==3:
print "\n\tProduct =", n1*n2
elif int(choice)==4:
print "\n\tQuotient =",n1/n2
elif int(choice)==5:
print "\n\tRemainder =",n1%n2
elif int(choice)==6:
print "\n\tExinting..."
exit()
main()
main()
d=(b**2)-(4*a*c)
if d<0:
print "\n\n\t\tNO REAL ROOTS FOUND!"
else:
print "\n\t\tSolution to equation - ",(((-1)*b)+(d**0.5))/(2*a),(((-1)*b)-(d**0.5))/
(2*a)
def main():
import math
print "\n\t\t\tMENU - OPTIONS"
print "\n\t\t1 --> Display natural nos."
print "\t\t2 --> Display even nos."
print "\t\t3 --> Display odd nos."
print "\t\t4 --> Exit"
for i in range(int(math.ceil(r1)),int(math.floor(r2))+1,1):
if choice==1:
print i
elif choice==2:
if i%2==0:
print i
elif choice==3:
if i%2!=0:
print i
elif choice==4:
print "Exiting..."
exit()
main()
main()
# 7 - menu driven pgm to find factors, factorial, and divisiblity test for 8
import math
def main():
if choice==1:
n=input("\n\tEnter a number - ")
print "\n\tFactors of",n,":"
for i in range(1,n+1,1):
if n%i==0:
print i
elif choice==2:
n=input("\n\tEnter a number - ")
print "\n\tFactorial of",n,"=",math.factorial(n)
elif choice==3:
n=input("\n\tEnter a number - ")
if n%8==0:
print "\n\tDivisible by 8 - 8 x",n/8,"=",n
else:
print "\n\tNot divisible by 8"
elif choice==4:
print "\n\tExiting..."
exit()
main()
main()
# 8 - menu driven pgm to check if no. is palindrome or armstrong and to print the
sum of digits
def main():
print "\n\t\tMENU - OPTIONS"
print "\n\t1 --> Palindrome check"
#9 - menu driven pgm to check if no. is even or odd and to check if it is prime
def main():
print "\n\t\tMENU - OPTIONS"
print "\n\t1 --> Prime check"
print "\t2 --> Even or odd no. check"
if choice==1:
for i in range(1,n,1):
if n%i==0:
print "\n\tNot a prime no."
break
else: print "\n\tPrime number"
elif choice==2:
if n%2==0:
print "\n\tEven number"
else: print "\n\tOdd number"
main()
main()
# 10 - menu driven pgm to print 'n' prime numbers & 'n' Armstrong nos.
while True:
print "\n\t\t\tMENU"
print "\t\t1 --> Print 'n' prime numbers"
print "\t\t2 --> Print 'n' Armstrong numbers"
print "\t\t3 --> Exit"
choice=input("\nEnter your choice - ")
if choice==1:
n=input("\nEnter the number of prime nos. required - ")
print "\n",n,"prime numbers:"
no=0; i=1
while i<=n:
l=[]
for j in range(1,no+1):
if no%j==0:
l.append(j)
if len(l)==2:
print "\n#",i,"-",no
i+=1
no+=1
elif choice==2:
n=input("\nEnter the number of Armstrong nos. required - ")
print "\n",n,"Armstrong numbers:"
i=1; no=0
while i<=n:
no_=no; s=0
while no_!=0:
a=no_%10
s+=a**(len(str(no)))
no_=no_/10
if s==no:
print "\n#",i,"-",no
i+=1
no+=1
elif choice==3:
print "\nExiting..."
exit()
else:
print "\nWrong choice! Try again!"
if choice==3:
print "\n\tExiting..."
exit()
n=input("\n\tEnter your number - ")
if choice==1:
if str(n)==str(n)[::-1]:
print "\n\tPalindrome"
else:
print "\n\tNot a palindrome"
elif choice==2:
s=0
for i in str(n):
s+=int(i)**len(str(n))
if s==n:
print "\n\tArmstrong Number"
else: print "\n\tNot an Armstrong number"
main()
main()
x=0;y=1
n=input("\n\tEnter the number of terms - ")
print "\n\tTerms of Fibonacci series"
print x
print y
for i in range(n-2):
s=x+y
print s
x=y
y=s
**
***"""
n=input("Enter the maximum number of rows - ")
for i in range(1,n+1):
print "*"*i,
print
for i in range(n,0,-1):
print "*"*i,
print
for i in range(1,m+1):
for j in range(n):
print i,
print
123"""
for i in range(m):
for j in range(1,n+1):
print j,
print
for i in range(1,m+1):
for j in range(1,i+1):
print j,
print
for i in range(1,m+1):
for j in range(m,i-1,-1):
print j,
print
for i in range(m,0,-1):
for j in range(1,i+1):
print j,
print
for i in range(1,m+1):
print " "*(m-i),
print "* "*i
print
for i in range(1,m+1):
print " "*(m-i),
for i in range(1,m+1):
print " "*(m-i),
for j in range(1,i+1):
print j,
for k in range(j-1,0,-1):
print k,
print
#28 - pgm to check if alphabets, digits, and/or, alphanumeric chs. are in str
import string
str=raw_input("\n\tEnter the string - ")
if str.isalpha():
#29 - pgm to find the number of alphabets, digits and spaces in a string
import string
str=raw_input("\n\tEnter the string - ")
str_a=str_d=str_s=0
for i in str:
if i.isalpha():
str_a+=1
elif i.isdigit():
str_d+=1
elif i.isspace():
str_s+=1
print "\n\tNumber of alphabets in string =",str_a
print "\n\tNumber of digits in string =",str_d
print "\n\tNumber of spaces in string =",str_s
#34 - pgm to enter 'n' values and print highest and lowest
n=input("\n\tEnter the number of values - ")
l=[]
for i in range(n):
j=float(input("\n\tEnter value #"+str(i+1)+" - "))
l.append(j)
print "\n\tHighest value =",max(l)
print "\n\tLowest value =",min(l)
#35 - pgm to enter 'n' values and print sum and average
l[int(round(float(n)/2)):],l[:int((n/2))]=l[:int((n/2))],l[int(round(float(n)/2)):]
#38 - menu driven pgm to create a list, and edit, append, delete, and display
elements
while True:
print "\n\t\t\t------MENU------"
print "\t\t1 --> Create a list"
print "\t\t2 --> Edit elements"
print "\t\t3 --> Append elements"
print "\t\t4 --> Delete elements"
print "\t\t5 --> Display elements"
print "\t\t6 --> Exit"
if choice==1:
n=input("\nEnter the number of elements - ")
l=[raw_input("\nEnter the element - ") for i in range(n)]
elif choice==4:
print "\n\tINDEX\t\tELEMENT"
for i in range(n):
print "\t"+str(i)+"\t\t"+str(l[i])
no=input("\nEnter the number of elements to be deleted - ")
for i in range(no):
choice=input("\nEnter the index of the element to be deleted - ")
del l[choice]
elif choice==3:
a=input("\nEnter the number of elements to be appended - ")
for i in a: l.append(raw_input("\nEnter the element - "))
elif choice==2:
print "\n\tINDEX\t\tELEMENT"
for i in range(n):
print "\t"+str(i)+"\t\t"+str(l[i])
no=input("\nEnter the number of elements to edit - ")
for i in range(no):
ind=input("\nEnter the index of the element to edit - ")
l[ind]=raw_input("\nEnter the element - ")
elif choice==5:
#39 - pgm to print elements above and below main and off diagonals of a matrix
def display(m,n,l):
for i in range(m):
for j in range(n):
print l[i][j],
print
def main():
m=input("Enter the number of rows - ")
n=input("Enter the number of columns - ")
l=[[input("\nEnter the element - ") for j in range(n)]for i in range(m)]
display(m,n,l)
print "\nElements above the the main diagonal..."
for i in range(m):
for j in range(n):
if j>i:
print l[i][j],
else: print " ",
print
print "\nElements below the the main diagonal..."
for i in range(m):
for j in range(n):
if j<i:
print l[i][j],
else: print " ",
print
print "\nElements above the the off diagonal..."
for i in range(m):
for j in range(n):
if j<m-1-i:
print l[i][j],
else: print " ",
print
print "\nElements below the the off diagonal..."
for i in range(m):
for j in range(n):
if j>m-1-i:
print l[i][j],
else: print " ",
print
main()
for i in range(m):
for j in range(n):
print l[i][j],
print
for i in range(m):
for j in range(n):
if (i==0 or i==m-1 or i==m/2) or (j==0 or j==n-1 or j==n/2):
print l[i][j],
else: print " ",
print
#41 - menu driven pgm to print main diagonal and off diagonal of a matrix using
single loop or otherwise
m=input("\nEnter the number of rows - ")
n=input("\nEnter the number of columns - ")
l=[[input("\nEnter the element - ") for j in range(n)]for i in range(m)]
print
for i in range(m):
for j in range(n):
print l[i][j],
print
while True:
print "\n\t\t---MENU---"
print "\n\t1 --> Main diagonal using nested loop"
print "\n\t2 --> Main diagonal using single loop"
print "\n\t3 --> Off diagonal using nested loop"
print "\n\t4 --> Off diagonal using single loop"
print "\n\t5 --> Exit"
if choice==1:
print
for i in range(m):
for j in range(n):
if j==i: print l[i][j],
else: print " ",
print
elif choice==2:
print
for i in range(m):
print " "*i,
print l[i][i]
elif choice==3:
print
for i in range(m):
for j in range(n):
if j==m-1-i: print l[i][j],
else: print " ",
print
elif choice==4:
print
for i in range(m):
print " "*(m-1-i),
print l[i][m-1-i]
elif choice==5:
print "\nExiting..."
exit()
for i in range(m):
for j in range(n):
l_add[i][j]=l1[i][j]+l2[i][j]
l_sub[i][j]=l1[i][j]-l2[i][j]
print "\nAddition of Matrices..."
display(m,n,l_add)
print "\nSubtraction of Matrices..."
display(m,n,l_sub)
main()
for i in range(m):
s_row=0
for j in range(n):
s_row+=l[i][j]
print "\nSum of Row",i+1,"=",s_row
for j in range(n):
s_col=0
for i in range(m):
s_col+=l[i][j]
print "\nSum of Column",j+1,"=",s_col
#44 - pgm to print the sum of elements above and below the main & off diagonals
m=input("\nEnter the number of rows - ")
n=input("\nEnter the number of columns - ")
l=[[input("\nEnter the element - ") for j in range(n)]for i in range(m)]
print
sum_a=sum_b=0
for i in range(m):
for j in range(n):
if j!=i:
sum_a+=l[i][j]
if j!=m-1-i:
sum_b+=l[i][j]
print l[i][j],
print
print "\nSum of elements above and below the main diagonal -",sum_a
print "\nSum of elements above and below the main diagonal -",sum_b
#47 - pgm to enter name and marks of students and search a student's mark
n=input("\nEnter the number of students to be added - ")
grades={}
for i in range(n):
name=raw_input("\nEnter the name of the student - ").lower().capitalize()
mark=raw_input("\nEnter the student's marks - ")
grades[name]=mark
print "\n\t\tSTUDENT\t\t\tMARKS"
for i in sorted(grades.keys()):
print "\n\t ",i,"\t\t\t",grades[i]
search=raw_input("\nEnter the student to search for his/her mark ").lower().capitalize()
print "\nResult",grades.get(search, "Student not available")