VIVEKANANDA EDUCATIONAL SOCIETY
CLASS : XI
COMPUTER SCIENCE - LAB MANUAL
ACADEMIC YEAR:
SN PRACTICAL DATE SIGNATURE
O
1 Write a program to calculate Simple Interest and
compound interest.
2 Write a program to check a number is positive or
negative or zero.
3 Write a program to input a character and to print
whether a given character is an alphabet, digit or any
other character.
4 Write a program to display ASCII code of a character
and vice versa.
5 Write a program in python to print the multiplication
table of a number
6 Write a program in python to check a number whether
it is prime or not.
7 Write a program to input the value of x and n and
print the sum of the following series:
a. 1 + x² + x³ + ... + xⁿ
b. x - x2/2! + x3/3! - x4/4! + x5/5! - x6/6!
8 Write a program to check a number whether it is
palindrome or not.
9 Write a Program to display the Fibonacci sequence up
to nth term where n is provided by the user.
10 Write a Python program to find the sum of natural
numbers up to n where n is provided by user.
11 Write a Python program that accepts a number from
the user and find given number is Armstrong or not.
12 WAP to accept a number and find out whether it is a
perfect number or not.
WAP to print the following pattern:
13
14 WAP to get decimal number as a input and convert
into binary number.
15 Write a python program to compute the greatest
common divisor and least common multiple of two
integers.
16 Write a Python program that accepts a word from the
user and reverse it.
17 Write a program to count and display the number of
vowels, consonants, uppercase, lowercase characters
in string.
18 WAP to calculate the total number of zeros, positive
and negative elements in the list.
19 Write a menu driven program for list manipulation.
Display menu as:
1. Add a record 2. View record 3. Delete Record
4. Modify record 5. Exit
20 Write a menu drive program to the following from the
list: 1. Maximum 2. Minimum 3.Sum
4. Sort in Ascending 5. Sort in Descending 6. Exit
21 WAP to find out the maximum and minimum element
from the list. (Don’t use any function)
22 Write a program to input a list of numbers and swap
elements at the even location with the elements at the
odd location.
23 Write a program to create a tuple with user input and
search for given element.
24 Write a program to create tuple with user input and
display the square of numbers divisible by 3 and
display cube of numbers divisible by 5.
25 Write a menu driven program to do the following using
a dictionary.
1. Display Contacts 2. Add Contact 3. Delete a Contact
4. Change a phone number 5. Search Contact 6. Exit
26 Write a program to create a dictionary and store
salesman name as a key and sales of 3 months as
values. Give the bonus to the salesman according to
the given criteria:
• Total Sales < 1K – No Bonus
• Total Sales 1K to 2K – 5%
• Total Sales 2.1K to 3K – 8%
• Total Sales 3.1K to 4K – 10%
• Total Sales >4K – 12%
27 Write a program to enter a team name, wins and
losses of a team. Store them into a dictionary where
teams names are key, wins and losses are values
stored as a list. Do the following based on the
dictionary created above:
• Find out the team’s winning percentage by their
names
• Display the no. of games won by each team in a list
• Display the no. of games lost by each team in a list
1. Aim: Write a program to calculate Simple Interest and compound interest.
Code:
p=float(input("Enter the principal amount : "))
r=float(input("Enter the rate of interest : "))
t=float(input("Enter the time in years : "))
si=p*t*r/100
x=(1+r/100)**t
CI= p*x-p
print("Simple interest is : ", round(si,2))
print("Compound interest is : ", round(CI,2))
Result:
The program was successfully executed and obtained the result.
2.Aim: Write a program to check a number is positive or negative or zero.
# In this program, we input a number # check if the number ispositive or #
negative or zero and display # an appropriatemessage # This time we use
nested if.
num = float(input("Enter a number: "))
ifnum>= 0:
ifnum == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
Result:
The program was successfully executed and obtained the result.
3.Aim: Write a program to input a character and to print whether a given
character is analphabet, digit or any other character.
Code:
ch=input("Enter a character: ")
ifch.isalpha():
print(ch, "is an alphabet")
elifch.isdigit():
print(ch, "is a digit")
elifch.isalnum():
print(ch, "is alphabet and numeric")
else:
print(ch, "is a special symbol")
Result:
The program was successfully executed and obtained the result.
4.Aim:Write a program to display ASCII code of a character and vice versa.
Code:
var=True
whilevar:
choice=int(input("Press-1 to find the ordinal value of a character \nPress-2 to
find a character of a value\n"))
if choice==1:
ch=input("Enter a character : ")
print(ord(ch))
elif choice==2:
val=int(input("Enter an integer value: "))
print(chr(val))
else:
print("You entered wrong choice")
print("Do you want to continue? Y/N")
option=input()
if option=='y' or option=='Y':
var=True
else:
var=False
Result:
The program was successfully executed and obtained the result.
5.Aim:Write a program in python to print the multiplication table ofa no.
Code:
num = 12
num = int(input("Display multiplication table of? "))
fori in range(1, 11):
print(num,'x',i,'=',num*i)
Result:
The program was successfully executed and obtained the result.
6.Aim. Write a program in python to check a number whether it is prime or
not.
Code:
num=int(input("Enter the number: "))
fori in range(2,num):
ifnum%i==0:
print(num, "is not prime number")
break;
else:
print(num,"is prime number")
Result:
The program was successfully executed and obtained the result.
7.Write a program to input the value of x and n and print the sum of the
following series:
a. 1 + x² + x³ + ... + xⁿ
b. x - x2/2! + x3/3! - x4/4! + x5/5! - x6/6!
x = float (input ("Enter value of x :"))
n = int (input ("Enter value of n :"))
s=0
#Series 1
for i in range (n+1) :
s += x**i
if i<n:
print(x**i,"+",end=" ")
else:
print(x**i,end=" ")
print ("=", s)
#Series 2
sum = 0
m=1
for i in range(1, 7) :
f=1
for j in range(1, i+1) :
f *= j
t = x ** i / f
if i==1:
print(int(t),end=" ")
elif i%2==0:
print("-",int(t),end=" ")
else:
print("+",int(t),end=" ")
sum += t * m
m = m * -1
print(" =", sum)
Result:
The program was successfully executed and obtained the result.
8. Aim:Write a program to check a number whether it is palindrome or not.
Code:
num=int(input("Enter a number : "))
n=num
res=0
whilenum>0:
rem=num%10
res=rem+res*10
num=num//10
if res==n:
print("Number is Palindrome")
else:
print("Number is not Palindrome")
Result:
The program was successfully executed and obtained the result.
9. Aim: Write a Program to display the Fibonacci sequence up to nthterm
where n is provided by the user.
Code:
nterms = int(input("How many terms? "))
# first two terms
n1 = 0
n2 = 1
count = 0
# check if the number of terms is valid
Ifnterms<= 0:
print("Please enter a positive integer")
elifnterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print (n1,n2, end=’ ‘)
print("Fibonacci sequence upto",nterms,":")
while count <nterms:
print(n1,end=' ')
n3 = n1 + n2
n1 = n2
n2 = n3
count += 1
Result:
The program was successfully executed and obtained the result.
10.Aim: Write a Python program to find the sum of naturalnumbers up to n
where n is provided by user.
Code:
num = int(input("Enter a number: "))
ifnum< 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate un till zero
while(num> 0):
sum += num
num -= 1
print("The sum is",sum)
Result:
The program was successfully executed and obtained the result.
11. Aim:Write a Python program that accepts a number from the user and
find given number is Armstrong or not.
Code:
num= int(input(“enter 3 digit number:”))
f=num
sum=0
while (f>0):
a=f%10
f=int(f/10)
sum=sum+(a**3)
if(sum==num):
print(“it is a Armstrong no:”,num)
else:
print(“it is not a Armstrong no:”,num)
Result:
The program was successfully executed and obtained the result.
12. Aim: WAP to accept a number and find out whether it is a perfect
number or not.
Code:
i=1
s=0
num=int(input(“Enter the number:”))
while i<num:
ifnum%i==0:
s+=i
i=i+1
if s==num:
print(“it is a perfect number”)
else:
print(“it is not a perfect number”)
Result:
The program was successfully executed and obtained the result.
13. WAP to print the following pattern:
*
**
***
****
*****
12345
1234
123
12
1
A
AB
ABC
ABCD
ABCDE
Code:
#Code For Pattern1
for i in range(1,6):
for j in range(1,i+1):
print("*",end=" ")
print()
#Code for Pattern2
for i in range(6,1,-1):
for j in range(1,i):
print(j,end=" ")
print()
#code for pattern 3
for i in range (65,70):
for j in range(65,i+1):
print(chr(j),end="")
print()
Result:
The program was successfully executed and obtained the result.
14.Aim: WAP to get decimal number as a input and convert into binary
number.
Code:
print("Enter Decimal Number: ", end="")
dnum = int(input())
bnum = 0
mul = 1
whilednum>0:
rem = dnum%2
bnum = bnum+(rem*mul)
mul = mul*10
dnum = int(dnum/2)
print("\nEquivalent Binary Value =", bnum)
Result:
The program was successfully executed and obtained the result.
15. Write a python program to compute the greatest common divisor and
least common multiple of two integers.
Code:
fn = int(input('Enter first number: '))
sn = int(input('Enter second number: '))
gcd = 1
for i in range(1,fn +1):
iffn%i==0 and sn%i==0:
gcd = i # Displaying GCD
print('HCF or GCD of %d and %d is %d' %(fn, sn,gcd)) # Calculating LCM
lcm = fn * sn/ gcd
print('LCM of %d and %d is %d' %(fn, sn, lcm))
Result:
The program was successfully executed and obtained the result.
16. Aim:Write a Python program that accepts a word from the user and
reverse it.
Code:
word = input("Input a word to reverse: ")
for char in range(len(word) - 1, -1, -1):
print(word[char], end="")
print("\n")
Result:
The program was successfully executed and obtained the result.
17. Write a program to count and display the number of vowels, consonants,
uppercase, lowercase characters in string.
Code:
txt = input("Enter Your String : ")
v = c = uc = lc = 0
v_list = ['a','e','i','o','u']
for i in txt:
if i in v_list:
v += 1
if i not in v_list:
c += 1
ifi.isupper():
uc += 1
ifi.islower():
lc += 1
print("Number of Vowels in this text = ", v)
print("Number of Consonants in this text = ", c)
print("Number of Uppercase letters in this text=", uc)
print("Number of Lowercase letters in this text=", lc)
Result:
The program was successfully executed and obtained the result.
18. Aim: WAP to calculate the total number of zeros, positive and negative
elements in the list.
Code:
l=[23,4,56,-1,0,46,-8,-9,0,-11,31,21]
z=0
p=0
n=0
for i in range(len(l)):
if l[i]==0:
z+=1
elif l[i]>0:
p+=1
else:
n+=1
print(“Total No. of zeros in the list are:”,z)
print(“Total No. of positive numbers in the list are:”,p)
print(“Total No. of negative numbers in the list are:”,p)
Result:
The program was successfully executed and obtained the result.
19. Aim: Write a menu driven program for list manipulation. Display menu
as: 1. Add a record 2. View record 3. Delete Record 4. Modify record 5. Exit
Code:
l=[]
while True:
print('''1. Add a record
2. View record
3. Delete Record
4. Modify record
5. Exit''')
ch=int(input("Enter your choice:"))
if ch==1:
v=int(input("Enter value to add a record:"))
l.append(v)
print("Record Added...")
print("List after insertion:",l)
elif ch==2:
print(l)
elif ch==3:
n=int(input("Enter the value to delete:"))
l.remove(n)
print("Record Deleted...")
print("List after deletion:",l)
elif ch==4:
i=int(input("Enter position to modify the value:"))
nv=int(input("Enter new value to modify:"))
l[i]=nv
print("Record Modified...")
print("List after modification”)
elif ch==5:
print(“Thank you! Good Bye”)
break
Result:
The program was successfully executed and obtained the result.
20. Aim: Write a menu drive program to the following from the list:
1. Maximum 2. Minimum 3.Sum 4. Sort in Ascending 5. Sort in Descending
6. Exit
Code:
l=[11,32,5,43,22,98,67,44]
while True:
print(''' 1. Maximum
2. Minimum
3. Sum
4. Sorting (Ascending)
5. Sorting (Descending)
6. Exit ''')
ch=int(input("Enter your choice:"))
ifch==1:
print("Maximum:",max(l))
elifch==2:
print("Minimum:",min(l))
elifch==3:
print("Sum:",sum(l))
elifch==4:
l.sort()
print("Sorted list(Ascending):",l)
elifch==5:
l.sort(reverse=True)
print("Sorted List(Descending:)",l)
elifch==6:
print("Thank you! Good Bye")
break
Result:
The program was successfully executed and obtained the result.
21.Aim: WAP to find out the maximum and minimum element from the list.
(Don’t use any function)
Code:
max_min=[0, 10, 15, 40, -5, 42, 17, 28, 75]
l = max_min [0]
s = max_min [0]
fornum in max_min:
if num> l:
l = num
elifnum< s:
s = num
print(“The maximum number is:”,l)
print(“The minimum number is:”, s)
Result:
The program was successfully executed and obtained the result.
22.Aim: Write a program to input a list of numbers and swap elements at the
even location with the elements at the odd location.
Code:
l=eval(input("Enter a list: "))
print("Original List before swapping:”,l)
s=len(l)
if s%2!=0:
s=s-1
for i in range(0,s,2):
l[i],l[i+1]=l[i+1],l[i]
print("List after swapping the values :",l)
Result:
The program was successfully executed and obtained the result.
23.Aim: Write a program to create a tuple with user input and search for
given element.
Code:
t=eval(input("Enter tuple here:"))
n=int(input("Enter element to search:"))
if n in t:
print("Found:",n)
else:
print("Tuple doesn't contain ",n)
Result:
The program was successfully executed and obtained the result.
24.Aim: Write a program to create tuple with user input and display the
square of numbers divisible by 3 and display cube of numbers divisible by 5.
t=eval(input("Enter tuple here:"))
for i in t:
if i%3==0:
print(i**2,end=' ')
if i%5==0:
print(i**3,end=' ')
Result:
The program was successfully executed and obtained the result.
25. Aim: Write a menu driven program to do the following using a dictionary.
1. Display Contacts
2. Add Contact
3. Delete a Contact
4. Change a phone number
5. Search Contact
6. Exit
Code:
d={}
while True:
print('''
1. Display Contacts
2. Add Contact
3. Delete a Contact
4. Change a phone number
5. Search Contact
6. Exit ''')
ch=int(input("Enter your choice:"))
if ch==1:
print("Saved Contacts.......")
for i in d:
print('----------------')
print("Name:",i)
print("Phone.No:",d[i])
print('----------------')
elif ch==2:
print("Add new contact")
name=input("Enter Name:")
ph=input("Enter Phone Number:")
#d[name]=ph
d.setdefault(name,ph)
print("Contact Saved...")
elif ch==3:
print("Delete existing contact")
n=input("Enter name to delete contact:")
#d.pop(n)
#d.popitem()
#del d[n]
d.clear()
print("Contact Deleted...")
elif ch==4:
print("Change a phone number")
n=input("Enter name to change phone no.:")
new_ph=input("Enter new phone number:")
d[n]=new_ph print("Contact Saved...")
elif ch==5:
print("Search Contact")
n=input("Enter name to search:")
if n in d:
print("Record Found...",d[n])
else:
print("Record not found...")
elif ch==6:
print("Quitting from App....")
input("Press Enter to Exit...")
break
Result:
The program was successfully executed and obtained the result.
26. Aim: Write a program to create a dictionary and store salesman name as
a key and sales of 3 months as values. Give the bonus to the salesman
according to the given criteria:
• Total Sales < 1K – No Bonus
• Total Sales 1K to 2K – 5%
• Total Sales 2.1K to 3K – 8%
• Total Sales 3.1K to 4K – 10%
• Total Sales >4K – 12%
Code:
d={'Rudra':[199,180,540],'Rushi':[543,876,453],
Preet':[650,987,123],'Jay':[284,456,321]}
bonus=0
for i in d:
d[i]=sum(d[i])
for i in d:
if d[i]=1000 and d[i]<=2000:
d[i]=d[i]*0.05
elif d[i]>=2001 and d[i]<=3000:
d[i]=d[i]*0.08
elif d[i]>=3001 and d[i]<=4000:
d[i]=d[i]*0.1
elif d[i]>=4001:
d[i]=d[i]*0.12
print("Bonus for salesman:")
for i in d:
print(i,"\t: %.2f"%d[i])
Result:
The program was successfully executed and obtained the result.
27. Aim: Write a program to enter a team name, wins and losses of a team.
Store them into a dictionary where teams names are key, wins and losses are
values stored as a list. Do the following based on the dictionary created
above:
• Find out the team’s winning percentage by their names
• Display the no. of games won by each team in a list
• Display the no. of games lost by each team in a list
Code:
di ={}
l_win = []
l_rec = []
while True :
t_name = input ("Enter name of team (q for quit): ")
ift_name in 'Qq' :
print()
break
else :
win = int (input("Enter the no.of win match: "))
loss = int(input("Enter the no.of loss match: "))
di [ t_name ] = [ win , loss ]
l_win += [ win ]
if win > 0 :
l_rec += [ t_name ]
n = input ("Enter name of team for winning percentage: ")
wp=di[n][0] *100 / (di[n][0] + di[n][1] )
print ("Winning percentage:%.2f"%wp)
print("Winning list of all team = ",l_win)
print("Team who has winning records are ",l_rec)
Result:
The program was successfully executed and obtained the result.