[go: up one dir, main page]

100% found this document useful (1 vote)
348 views62 pages

Computer Class XI Programs For Term-1 and Term-2

Uploaded by

prince2020me1
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
100% found this document useful (1 vote)
348 views62 pages

Computer Class XI Programs For Term-1 and Term-2

Uploaded by

prince2020me1
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
You are on page 1/ 62

NOTE :

GO THROUGH ALL PROGRAMS.


WRITE ANY 10 PROGRAMS FROM 1-26 (IF, FOR, WHILE )

WRITE ANY 3 PROGRAMS FROM 1-7 (STRING)

WRITE ANY 3 PROGRAMS FROM 1-7 (LIST)

WRITE ANY 3 PROGRAMS FROM 1-7 (TUPLE)

WRITE ANY 3 PROGRAMS FROM 1-7 (DICTIONARY)


1. Write a program to print table of a
number entered from the user.
num = int(input("Enter any number"))
for i in range(1,11):
print(num," * ", i," = ", num *i)
OUTPUT :
Enter any number7
7*1=7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70
2. Write a program to display all even
numbers that falls between two numbers
(exclusive both numbers) entered from
the user.
Ans.

num1 = int(input("Enter first number"))


num2 = int(input("Enter second number"))
if num1 > num2 :
for i in range(num2+1 , num1):
if i%2 == 0:
print(i)
else :
for i in range(num1 + 1, num2):
if i % 2 == 0:
print(i)

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)

OUTPUT : Enter any number : 6


Factorial of a number is : 720
9. Write a program to print the following
pattern:

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

num1 = input("Enter any number : ")


L1 = list(num1)
L = len(L1)
i=0
n = {0 : "Zero", 1 : "One", 2 : "Two", 3 : "Three", 4
: "Four", 5 : "Five", 6 : "Six", 7 : "Seven", 8 :
"Eight", 9 : "Nine"}
while (i < L):
print(n[int(L1[i])], end = " ")
i=i+1

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.)

num1 = input("Enter any number : ")


p=0
L = len(num1)
L1 = list(num1)
i=0
while(i < L):
p = p + int(L1[i])**3
i=i+1
if int(num1) == p:
print("Number is Armstrong")
else:
print("Number is not Armstrong")

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!

num = int(input("Enter any number : "))


sum = 0
fact = 1
i=1
while(i < num):
fact= fact*i
sum= sum + i/fact
i=i+1
print("Sum is : ",round(sum, 2))

OUTPUT :

Enter any number : 3


Sum is : 2.0
14. Write a program to enter the
numbers till the user wants and at the
end it should display the sum of all the
numbers entered.

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 :

Enter any number : 1


Do you wish to continue(Y,N)?: y
Enter any number : 2
Do you wish to continue(Y,N)?: y
Enter any number : 3
Do you wish to continue(Y,N)?: n
Sum of all the numbers is : 6
15. Write a program to enter the numbers till the user
enter ZERO and at the end it should display the count of
positive and negative numbers entered.

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.

num1 = int(input("Enter first number : "))


num2 = int(input("Enter second number : "))
rem = 1
if num1 > num2 :
while rem!=0:
rem = num1 % num2
if rem == 0:
hcf = num2
else:
num1 = num2
num2 = rem
else :
while rem!=0:
rem = num2 % num1
if rem == 0:
hcf = num1
else:
num2 = num1
num1 = rem
print("HCF of two numbers is : ", hcf)

OUTPUT :

Enter first number: 2


Enter second number: 8
HCF is 2
17. Write a program to convert Decimal
to Binary.

num = int(input("Enter a positive number: "))


bin = 0
p=1
n = num
while n>0:
rem = int(n % 2)
bin = bin + rem * p
p = p*10
n = n/2
print("Binary number of",num,"is",bin)

OUTPUT :

Enter a positive number: 15


Binary number of 15 is 1111
18. Write a program to convert Binary to
Decimal.

bin = input("Enter a binary number: ")


dec=0
p=0
for i in reversed(bin):
dec = dec + int(i)*pow(2,p)
p=p+1
print("Decimal number of binary",bin," number is : ",dec)

OUTPUT :

Enter a binary number: 1111


Decimal number of binary 1111 number is : 15
19. Write a program to check whether a
number is palindrome or not.

num1 = int(input("Enter any number : "))


onum = num1
r=0
rnum=0
len = len(str(num1))
i=0
while(i<len):
r = num1 % 10
rnum = rnum * 10 + r
num1 = num1//10
i=i+1
if onum == rnum:
print("Number is Palindrome")
else:
print("Number is not Palindrome")

OUTPUT :

Enter any number : 1234321


Number is Palindrome
Enter any number : 123432
Number is not Palindrome
20. Write a python program to sum the
sequence:
1 + 1/1! + 1/2! + 1/3! + …….. + 1/n!

n = int(input("enter a nth term = "))


f=1
s=1
i=1
while(i<=n):
f=f*i
s=s+1/f
i=i+1
print("sum of sequence = ", s)

OUTPUT :

enter a nth term = 5


sum of sequence = 2.7166666666666663
21. Write a program to print the
following series till n terms.

2 , 22 , 222 , 2222 _ _ _ _ _ n terms

n = int(input("Enter value of n : "))


st = '2'
i=0
while(i<n):
print(st, end=" , ")
st =st + '2'
i=i+1

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.

n = int(input("Enter value of n : "))


i=1
while(i <= n):
print(i**2, end = " , ")
i=i+1

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

n = int(input("enter a nth term = "))


x = int(input("Enter value of x : "))
sum = 0
i=1
while(i<=n):
sum = sum + 2**i/i
i=i+1
print(round(sum,2))

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

n = int(input("Enter number of terms : "))


s=0
i=1
while(i <= n):
s = s + i ** 3
i=i+1
print(s)

OUTPUT:

Enter number of terms : 4


100
25. Write a program to find the sum of
following series:

1 + 2 + 6 + 24 + 120 . . . . . n terms

n = int(input("Enter number of terms : "))


s=0
pr = 1
i=1
while(i<=n) :
pr = i * pr
print(pr, end = " + ")
s = s + pr
i=i+1

print(" = ", s)

OUTPUT :

Enter number of terms : 5


1 + 2 + 6 + 24 + 120 + = 153
26. Write a program to find the sum of
following series:

S = 1 + 4 – 9 + 16 – 25 + 36 – … … n terms

n = int(input("Enter number of terms : "))


s=0
sp = 1
sn = 0
i=2
while(i <= n):
if i % 2 == 0:
sp = sp + i ** 2
i=i+1
else :
sn = sn + i ** 2
i=i+1
print(sp - sn)

OUTPUT :

Enter number of terms : 6


23
1. Write a program to count number
of small case alphabets in String.

str = input("Enter any String")


L=0
for i in str:
if i.islower( ):
L = L+1
print("Total small case alphabets are : ",L)

OUTPUT
Enter any StringRadaR
Total small case alphabets are : 3
2. Write a program to count the
number of words in the input string.

str = input("Enter any String")


L=str.split()
print("Total number of words are ", len(L))

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‟.

str = input("Enter any String")


a=0
c=0
for i in str:
if i=='a':
a+=1
if i=='c':
c+=1
print("Total number of alphabet 'a' is ", a)
print("Total number of alphabet 'c' is ", 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”.

str = input("Enter any String")


w=str.split()
c=0
for i in w:
if i[0]=='a' or i[0]=='A':
c=c+1
print("Total words starting from 'a' or 'A' : ",c)

OUTPUT :

Enter any String Anish likes apple and mango


Total words starting from 'a' or 'A' : 3
5. Write a program to count number
of digits in a string accepted from
user.

str = input("Enter any String")


d=0
for i in str:
if i.isdigit():
d=d+1
print("Total digits are : ",d)

OUTPUT :

Enter any StringAan23$


Total digits are : 2
6. Write a function in python which
accept a string as argument and
display total number of vowels.

str=input(" Enter a string ")


v=0
for i in range(len(str)):
if str[i] in "aeiouAEIOU":
v = v +1
print("Total number of vowels are ", v)

OUTPUT :

Enter a string science


Total number of vowels are 3
7. Write a program to display the
largest word from the string

str=input("Enter any String :")


L = str.split()
max=0
b=""
for i in L:
if len(i) > max:
b=i
max=len(i)
print("Longest word is ",b)

OUTPUT :

Enter any String :air soil water


Longest word is water
1. Write a python prg to display all
the names from a list of names which
are starting from alphabet „p‟.

name = ['Amit' , 'Sumit', 'Pooja' , 'Mini' , 'Ronit' , 'Abdul', 'Poonam']

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.

L = [„Amit‟ , „Suman‟, 4, 8, „Jack‟, 9]

L = ['Amit' , 'Suman', 4, 8, 'Jack', 9]


c=0
for i in L:
if str(i).isdigit():
L[c]=L[c]*3
c=c+1
print(L)

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:

Original List : L = [23, 45, 67, 29, 12, 1]


After Swaping : L = [45,23, 29, 69, 1, 12]

L = [23, 13, 101, 6, 81, 9, 4, 1]


for i in range(0, len(L), 2):
L[i], L[i+1] = L[i+1], L[i]
print(L)

OUTPUT:
[13, 23, 6, 101, 9, 81, 1, 4]
6. Write a program in python which
concatenate all the numbers present
in the list.

Original List : [12, 34, 43, 2, 34]

Output = 123443234

L = [34, 23, 21, 49, 7, 8]


r=""
for i in L:
r = r + str(i)
print(r)

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

L = ["Amit" , "Suman" , "Sumit" ,


"Montu" , "Anil"]
for i in L:
if i[-2]=='i':
print(i)

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)

# printing original tuple


print("The original tuple : " + str(test_tup))

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

tuple1 = (50, 10, 60, 70, 50)


print('The no of value 50 is ', tuple1.count(50))

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()]

n=int(input("Enter how many email id to enter : "))


t1=()
un = ()
dom = ()
for i in range(n):
em = input("Enter email id : ")
t1 = t1 + (em,)
for i in t1:
a = i.split('@')
un = un + (a[0],)
dom = dom + (a[1],)
print("Original Email id's are : " , t1)
print("Username are : " , un)
print("Doman name are : " , dom)
OUTPUT :

Enter how many email id to enter : 2


Enter email id : csip@gmail.com
Enter email id : bike@yahoo.com
Original Email id's are : ('csip@gmail.com',
'bike@yahoo.com')
Username are : ('csip', 'bike')
Doman name are : ('gmail.com', 'yahoo.com')
7. Write a Python program to convert a
list to a tuple.

#Convert list to tuple


listx = [5, 10, 7, 4, 15, 3]
print(listx)
#use the tuple() function built-in Python,
passing as parameter the list

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

Original Dictionary = {1 : 25, 2 : 21, 3 : 23}


Expected Output = [21, 25, 32]

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”}
New dictionary = {1: “Amit”, 2 : “Suman”, 4 : “Ravi”, 5 : “Kamal”}

d1 = {1 :"Amit",2 : "Suman"}
d2 = {4:"Ravi", 5 : "Kamal"}
d3={ }
for i in (d1,d2):
d3.update(i)
print(d3)

OUTPUT :

{1: 'Amit', 2: 'Suman', 4: 'Ravi', 5: 'Kamal'}


3. Write a python program to take a key
and check whether that key is present in
dictionary or not.

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

{Empid : (Empname, EmpSalary}

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

You might also like