Computer Class XI Programs For Term-1 and Term-2
Computer Class XI Programs For Term-1 and Term-2
OUTPUT :
Enter first number34
Enter second number12
14 16 18 20 22 24 26 28 30 32
3. Write a program to check whether a
number is prime or not.
num1 = int(input("Enter any number : "))
k=0
if num1 == 0 or num1 == 1:
print("Not a prime number ")
else:
for i in range(2,num1):
if num1 % i == 0:
k = k+1
if k == 0 :
print(num1, "is prime number")
else:
print(num1, "is not prime number")
OUTPUT :
Enter any number : 5
5 is prime number
Enter any number : 6
6 is not prime number
4. Write a program to find the sum of the
digits of a number accepted from the
user.
num1 = int(input("Enter any number : "))
r=0
s=0
len = len(str(num1))
for i in range(len):
r = num1 % 10
s=s+r
num1 = num1//10
print("Sum of the digits are : " , s)
OR
num = input("Enter any number : ")
f=0
for i in num:
f = f + int(i)
print("Sum of the digits is : ", f)
OUTPUT :
Enter any number : 564
Sum of the digits are : 15
5. Write a program to find the product of
the digits of a number accepted from the
user.
num = input("Enter any number : ")
f=1
for i in num:
f = f * int(i)
print("Product of the digits is : ", f)
OUTPUT :
Enter any number : 231
Product of the digits is : 6
6. Write a program to reverse the number
accepted from user.
num1 = int(input("Enter any number : "))
r=0
rnum=0
while num1>0:
r = num1 % 10
rnum = rnum * 10 + r
num1 = num1//10
print("Reverse number is : ", rnum)
OUTPUT :
Enter any number : 12345
Reverse number is : 54321
7. Write a program to print the Fibonacci
series till n terms (Accept n from user)
n = int(input("How many terms you want in Fibinacci
Series"))
if n==1:
print("1")
elif n == 2:
print("1, 1")
elif n <= 0:
print("Please enter positive number greater than 0")
else:
ft = 1
st = 1
print(ft,end=" ")
print(st,end=" ")
for i in range(2,n):
nt = ft + st
print(nt,end = " ")
ft = st
st = nt
OUTPUT :
How many terms you want in Fibinacci Series 5
11235
8. Write a program to print the factorial of
a number accepted from user.
num = int(input("Enter any number : "))
f=1
for i in range(1,num+1):
f=f*i
print("Factorial of a number is : ", f)
1
12
123
1234
12345
Code:
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end=' ')
print('')
10. Write a program to print the following
pattern:
*
* *
* * *
* * * *
* * * * *
for i in range(1,6):
for j in range(i):
print(" * ", end = "")
print( )
11. Write a program to display the number
names of the digits of a number entered by
user, for example if the number is 231 then
output should be Two Three One
OUTPUT :
Enter any number : 937
Nine Three Seven
12. Write a program to check whether a number
is Armstrong or not. (Armstrong number is
a number that is equal to the sum of cubes of
its digits for example : 153 = 1^3 + 5^3 + 3^3.)
OUTPUT :
Enter any number : 153
Number is Armstrong
Enter any number : 135
Number is not Armstrong
13. Write a program to add first n terms of
the following series using a for loop:
1/1! + 1/2! + 1/3! + …….. + 1/n!
OUTPUT :
ch = 'Y'
sum = 0
while ch.upper() == 'Y':
num = int(input("Enter any number : "))
sum = sum + num
ch=input("Do you wish to continue(Y,N)?: ")
print("Sum of all the numbers is : ", sum)
OUTPUT :
ch = 'Y'
sump = 0
sumn = 0
num=1
while (num!=0):
num = int(input("Enter any number : "))
if num > 0 :
sump = sump + num
else:
sumn = sumn + num
print("Sum of all the positive numbers is : ", sump)
print("Sum of all the negative numbers is : ", sumn)
OUTPUT :
Enter any number : 5
Enter any number : -5
Enter any number : 3
Enter any number : -3
Enter any number : 4
Enter any number : -4
Enter any number : 0
Sum of all the positive numbers is : 12
Sum of all the negative numbers is : -12
16. Write a program to find the HCF of
two numbers entered from the user.
OUTPUT :
OUTPUT :
OUTPUT :
OUTPUT :
OUTPUT :
OUTPUT :
Enter value of n : 5
2 , 22 , 222 , 2222 , 22222 ,
22. Write a program to print the
following series till n terms.
1 4 9 16 25 _ _ _ _ _ n terms.
OUTPUT :
Enter value of n : 5
1 , 4 , 9 , 16 , 25 ,
23. Write a program to find the sum
of following series :
x + x2/2 + ……….xn/n
OUTPUT :
enter a nth term = 3
Enter value of x : 2
6.67
24. Write a program to find the sum
of following series
1 + 8 + 27 …………n terms
OUTPUT:
1 + 2 + 6 + 24 + 120 . . . . . n terms
print(" = ", s)
OUTPUT :
S = 1 + 4 – 9 + 16 – 25 + 36 – … … n terms
OUTPUT :
OUTPUT
Enter any StringRadaR
Total small case alphabets are : 3
2. Write a program to count the
number of words in the input string.
OUTPUT
Enter any StringI Read in class xi
Total number of words are 5
3. Write a program to accept a string
from the user and count the frequency of
alphabet „a‟ and „c‟.
OUTPUT :
Enter any String cream cash
Total number of alphabet 'a' is 2
Total number of alphabet 'c' is 2
4. Write a program to display those
words from the string in python
which are starting from alphabet „a‟
or „A”.
OUTPUT :
OUTPUT :
OUTPUT :
OUTPUT :
for i in name:
if i[0]=='p' or i[0]=='P':
print(i)
OUTPUT:
Pooja
Poonam
2. Write a python program to display
all the names from a list of names
whose length is of four characters.
name = ['Amit' , 'Sumit', 'Pooja' , 'Mini' , 'Ronit' , 'Abdul', 'Poonam']
for i in name:
if len(i)==4:
print(i)
OUTPUT:
Amit
Mini
3. Write a program in python which
multiply all the numbers in the list by 3.
OUTPUT:
['Amit', 'Suman', 12, 24, 'Jack', 27]
4. Write a program in python which
convert all the odd numbers in the list
to even by adding 1.
L = [13,21,32,24,55,26,17,38,59]
for i in range(len(L)):
if L[i] % 2 != 0:
L[i] = L[i]+1
print(L)
OUTPUT :
[14, 22, 32, 24, 56, 26, 18, 38, 60]
5. Write a program in python which swap
the alternate members of list (assuming
that even number of elements in the list).
like as shown below:
OUTPUT:
[13, 23, 6, 101, 9, 81, 1, 4]
6. Write a program in python which
concatenate all the numbers present
in the list.
Output = 123443234
OUTPUT:
3423214978
7. Write a program in python which
display only those names from the list
which have „i‟ as second last character.
like L = [“Amit”, “Suman”, “Sumit”,
“Montu” , “Anil”]
Output :
Amit
Sumit
OUTPUT :
Amit
Sumit
Anil
1. Write a python program to Check if the given
element is present in tuple or not.
test_tup = (10, 4, 5, 6, 8)
N = int(input("value to be checked:"))
res = False
for ele in test_tup :
if N == ele :
res = True
break
print("Does contain required value ? : " + str(res))
OUTPUT :
The original tuple : (10, 4, 5, 6, 8)
value to be checked:9
Does contain required value ? : False
The original tuple : (10, 4, 5, 6, 8)
value to be checked:4
Does contain required value ? : True
2. Write a python program to Counts the
number of occurrences of item 50 from a tuple
OUTPUT :
The no of value 50 is 2
3. Write a Python program to add an item
in a tuple in different ways.
tuplex = (4, 6, 2, 8, 3, 1)
print(tuplex)
tuplex = tuplex + (9,)
print(tuplex)
#adding items in a specific index
tuplex = tuplex[:5] + (15, 20, 25) + tuplex[:5]
print(tuplex)
#converting the tuple to list
listx = list(tuplex)
#use different ways to add items in list
listx.append(30)
tuplex = tuple(listx)
print(tuplex)
OUTPUT :
(4, 6, 2, 8, 3, 1)
(4, 6, 2, 8, 3, 1, 9)
(4, 6, 2, 8, 3, 15, 20, 25, 4, 6, 2, 8, 3)
(4, 6, 2, 8, 3, 15, 20, 25, 4, 6, 2, 8, 3, 30)
4. Write
a Python program to find the
repeated items of a tuple.
#create a tuple
tuplex = 2, 4, 5, 6, 2, 3, 4, 4, 7
print(tuplex)
#return the number of times it appears in the tuple.
count = tuplex.count(4)
print(count)
OUTPUT :
(2, 4, 5, 6, 2, 3, 4, 4, 7)
3
5.Write a Python program to check
whether an element exists within a tuple.
tuplex = ("w", 3, "r", "e", "s", "o", "u", "r", "c", "e")
print("r" in tuplex)
print(5 in tuplex)
OUTPUT :
True
False
6. Write a program to read email IDs of n number of
students and store them in a tuple. Create two new
tuples, one to store only the usernames from the
email IDs and second to store domain names from
the email ids. Print all three tuples at the end of the
program. [Hint: You may use the function split()]
tuplex = tuple(listx)
print(tuplex)
OUTPUT :
[5, 10, 7, 4, 15, 3]
(5, 10, 7, 4, 15, 3)
1. Write a Python program to arrange the
values in a dictionary in ascending order.
For example
d={1:21,2:32,3:25}
print(sorted(d.values()))
print(d)
OUTPUT :
[21, 25, 32]
{1: 21, 2: 32, 3: 25}
2. Write a python programs to
join/merge/concatenate the following two
dictionaries and create the new dictionary.
d1 = {1 :"Amit",2 : "Suman"}
d2 = {4:"Ravi", 5 : "Kamal"}
d3={ }
for i in (d1,d2):
d3.update(i)
print(d3)
OUTPUT :
d1 = {1 :'Amit',2 : 'Suman'}
i=int(input('Enter a key :'))
j=0
for k in d1:
if k==i:
print('key is present')
j=1
break
if j==0:
print('key is not present')
OUTPUT :
Enter a key :4
key is not present
Enter a key :2
key is present
4. Write a program to accept a key from
the user and remove that key from the
dictionary if present.
d1 = {1 : 2, 2 : 90, 3 : 50}
k=int(input('Enter any key'))
if k in d1:
d1.pop(k)
print(d1)
else:
print('Key not found')
OUTPUT :
Enter any key1
{2: 90, 3: 50}
Enter any key5
Key not found
5. Write a program in python to remove the
duplicate values from the dictionary. For
example:
Original dictionary = {1 : “Aman”, 2: “Suman”, 3: “Aman”}
New dictionary = {1 : “Aman”, 2: “Suman”}
d1={1:'Aman',2:'Suman',3:'Aman'}
nd1={ }
for k,v in d1.items():
if v not in nd1.values():
nd1[k]=v
print(nd1)
OUTPUT :
{1: 'Aman', 2: 'Suman'}
6. Write a program to accept the employee id from
the user and check Salary. If salary is less than 25000
then increase the salary by Rs1000. Data is stored in
the dictionary in the following format
emp={1:('Amit',25000),2:('Suman',30000),3:('Ravi',36000)}
pid=int(input('Enter the product id'))
l=[ ]
if pid in emp:
l=emp[pid]
if l[1]>25000:
emp[pid]=(l[0],l[1]+500)
print(emp)
OUTPUT :
Enter the product id1
{1: ('Amit', 25000), 2: ('Suman', 30000), 3: ('Ravi', 36000)}
Enter the product id2
{1: ('Amit', 25000), 2: ('Suman', 30500), 3: ('Ravi', 36000)}
7. Write a program to accept roll number, names and marks of
two students and store the detail in dictionary using roll number
as key. Also display the dictionary and sum of marks of two
students.
d={ }
s=0
for i in range(2):
rn = int(input('Enter roll number '))
nm = input('Enter name ')
mrk = input('Enter marks ')
temp=(nm, mrk)
d[rn]=temp
for i in d:
L=d[i]
s=s+int(L[1])
print('Dictionary is ', d)
print('Sum of marks ', s)
OUTPUT :
Enter roll number 1
Enter name Souvik
Enter marks 30
Enter roll number 2
Enter name Vijay
Enter marks 50
Dictionary is {1: ('Souvik', '30'), 2: ('Vijay', '50')}
Sum of marks 80