Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

National Public School Whitefield

COMPUTER
SCIENCE

Practical Record Book


ACADEMIC YEAR 2023-24

Name: Aman Thimmaiah


Class: 11 B
Roll: 5
INDEX
Program No. Program
1 Pattern printing
2 First 20 Twin prime
3 Count of characters
4 Menu driven
5 Integer string addition
6 Reversed words sentence
7 Formula bracket checking
8 Frequency of characters in list
9 Check for number in list
10 Print positive, negative no. In list
COMPUTER SCIENCE
PRACTICAL EXERCISES 2023-2024
CLASS-XI
Ex.no:1
Date:
Creating a program to print patterns
AIM:
To write a program to print two patterns
SOURCE CODE:
print("Pattern a: ")
x=3
c=1
for i in range(0,4):
print(' '*x,end='')
print('*'*c)
c=c+2
x=x-1
c=5
x=1
for k in range(3,0,-1):
print(' '*x,end='')
print('*'*c)
c=c-2
x=x+1
print()
print("Pattern b: ")
c=7
x=0
for k in range(4,1,-1):
print(' '*x,end='')
print('*'*c)
c=c-2
x=x+1
x=3
c=1
for i in range(0,4):
print(' '*x,end='')
print('*'*c)
c=c+2
x=x-1
SAMPLE OUTPUT:

Python program executed output:

Pattern a:
*
***
*****
*******
*****
***
*

Pattern b:
*******
*****
***
*
***
*****
*******
COMPUTER SCIENCE
PRACTICAL EXERCISES 2023-2024
CLASS-XI
Ex.no:2
Date:
Creating a program to print twin primes
AIM:
To write a program to print first 20 twin primes
SOURCE CODE:
print("first 20 twin prime numbers: ")
c=0
i=1
while c<=20:
while i>0:
i+=1
x = i+2
F=0
for k in range(2,i):
if i%k ==0:
F =1
for k in range(2,x):
if x%k==0:
F =1
if(F==0):
c+=1
print(i,x)
break
SAMPLE OUTPUT:

first 20 twin prime numbers:


35
57
11 13
17 19
29 31
41 43
59 61
71 73
101 103
107 109
137 139
149 151
179 181
191 193
197 199
227 229
239 241
269 271
281 283
311 313
347 349
COMPUTER SCIENCE
PRACTICAL EXERCISES 2023-2024
CLASS-XI
Ex.no:3
Date:
Creating a program to count characters
AIM:
To write a program to print no of, alphabets, digits and special characters
and word in the sentence.
SOURCE CODE:
n = input("enter a sentence: ")
L = n.split()
wrd =len(L)
tot=0
alf=0
dig=0
spc=0

for i in n:

if i.isalpha():
alf+=1
tot+=1
if i.isdigit():
dig+=1
tot+=1
if i.isascii() and not i.isalnum():
spc+=1
tot+=1

print("total |alpha |digits |special |words")


print(tot,' ',alf,' ',dig,' ',spc,' ',wrd)
SAMPLE OUTPUT:

Run 1:

enter a sentence: #$JFM 34med #)$k23


total |alpha |digits |special |words
18 7 4 7 3

Run 2:

enter a sentence: abc123%# ijk345%( 39dm..2


total |alpha |digits |special |words
25 8 9 8 3
COMPUTER SCIENCE
PRACTICAL EXERCISES 2023-2024
CLASS-XI
Ex.no:4
Date:
Creating a menu driven program
AIM:
To write a program to print fibonacci, factorial, or mersenne with on
menu driven program
SOURCE CODE:
n = int(input("enter 1 for fibonacci series till n terms, enter 2 for factorial of
n, enter 3 for mersenne numbers: "))
if n ==1:
num = int(input())
x=0
y=1
z=0
print(0,1,end=' ')
for i in range(num-2):
z=x+y
x=y
y=z
print(z,end=' ')

if n==2:
s=1
num = int(input())
for i in range(1,num+1):
s = s*i
print(s)
if n==3:
num = int(input())
for i in range(1,num+1):
print((2**i)-1)
SAMPLE OUTPUT:

Run 1:

enter 1 for fibonacci series till n terms, enter 2 for factorial


of n, enter 3 for mersenne numbers: 1
8
0 1 1 2 3 5 8 13

Run 2:

enter 1 for fibonacci series till n terms, enter 2 for factorial


of n, enter 3 for mersenne numbers: 2
5
120

Run 3:

enter 1 for fibonacci series till n terms, enter 2 for factorial


of n, enter 3 for mersenne numbers: 3
4
1
3
7
15
COMPUTER SCIENCE
PRACTICAL EXERCISES 2023-2024
CLASS-XI
Ex.no:5
Date:
Creating a program to add integers and string digits
AIM:
To write a program to print sum of number and digits of a string
SOURCE CODE:
n = int(input("enter an integer: "))
str = input("enter a string: ")
num=''
for i in str:
if i.isdigit():
num = num+i
if num!='':
num = int(num)
else:
num=0
print(n+num)
SAMPLE OUTPUT:

Run 1:

enter an integer: 20
enter a string: 567
587

Run 2:

enter an integer: 20
enter a string: a5b6c7
587

Run 3:

enter an integer: 20
enter a string: abc
20
COMPUTER SCIENCE
PRACTICAL EXERCISES 2023-2024
CLASS-XI
Ex.no:6
Date:
Creating a program to reverse words in sentence
AIM:
To write a program to print each word of sentence reversed
SOURCE CODE:
n = input("enter a sentence: ")
c = n.split()
c2=[]
x = len(c)
for i in range(x):
p=''
z=c[i]
l = len(z)
for k in range(l):
p = z[k]+p
c2.append(p)
for i in c2:
print(i,end=' ')
SAMPLE OUTPUT:

Run 1:

enter a sentence: Im going outside


mI gniog edistuo

Run 2:

enter a sentence: I will go Outside%#$


I lliw og $#%edistuO

Run 3:

enter a sentence: I am Going Out234side


I ma gnioG edis432tuO
COMPUTER SCIENCE
PRACTICAL EXERCISES 2023-2024
CLASS-XI
Ex.no:7
Date:
Creating a program to check for brackets
AIM:
To write a program to check a formula for correct number of brackets
SOURCE CODE:
n = input("Enter a formula: ")
op =0
cl=0
for i in n:
if i=='(':
op+=1
if i==')':
cl+=1
if op==cl:
print("formula has same number of opening and closing parenthesis")
else:
print("not the same number of opening and closing parenthesis")
SAMPLE OUTPUT:

Run 1:

Enter a formula: 40-(x+3(k*4-(2/5)))


formula has same number of opening and closing
parenthesis

Run 2:

Enter a formula: (2-5)+(x-4(4-3)))


not the same number of opening and closing parenthesis

Run 3:

Enter a formula: ((233-45)x-(x(2-3(530-34))


not the same number of opening and closing parenthesis

Run 4:

Enter a formula: ((233-45)x-(x(2-3(530-34))))


formula has same number of opening and closing
parenthesis
COMPUTER SCIENCE
PRACTICAL EXERCISES 2023-2024
CLASS-XI
Ex.no:8
Date:
Creating a program to find frequency of chars in list
AIM:
To write a program to check a list and print frequency and duplicates
SOURCE CODE:
n = eval(input("enter a list : "))
n1=[]
n2=[]
n3={}
for i in range(len(n)):
if n[i] in n3:
n3[n[i]] +=1
else:
n3[n[i]] =1
if n[i] not in n1:
n1.append(n[i])

if n3[n[i]]>1:
if n[i] in n1:
if n[i] not in n2:
n2.append(n[i])

print("list of unique elements: ",n1)


print("duplicates",n2)
print("freq of each element",n3)
SAMPLE OUTPUT:

Run 1:
enter a list : [1,2,3,4,5,6,7,8,9]
list of unique elements: [1, 2, 3, 4, 5, 6, 7, 8, 9]
duplicates [2, 5, 7, 9]
freq of each element {1: 1, 2: 2, 3: 1, 4: 1, 5: 2, 6: 1, 7: 3,
8: 1, 9: 4}

Run 2:
enter a list : [1,2,3,4,5,6,7,8,9]
list of unique elements: [1, 2, 3, 4, 5, 6, 7, 8, 9]
duplicates []
freq of each element {1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1,
8: 1, 9: 1}

Run 3:
enter a list : [1,1,2,3,4,5,5,6,7,8,8,9,9,9]
list of unique elements: [1, 2, 3, 4, 5, 6, 7, 8, 9]
duplicates [1, 5, 8, 9]
freq of each element {1: 2, 2: 1, 3: 1, 4: 1, 5: 2, 6: 1, 7: 1,
8: 2, 9: 3}
COMPUTER SCIENCE
PRACTICAL EXERCISES 2023-2024
CLASS-XI
Ex.no:9
Date:
Creating a program to find number in list
AIM:
To write a program to check a list and find position of number
SOURCE CODE:
n = eval(input("enter a list of numbers: "))
k = int(input("enter a number to check for: "))
x=[]
c=0
for i in range(len(n)):
if k == n[i]:
x.append(i+1)
c+=1
if c==0:
print('number not found in list')
else:
print("number",k,"found at position: ",x)
SAMPLE OUTPUT:

Run 1:
enter a list of numbers: [1,2,3,4,5,4,6,7,5,8,5,43,3,2,1]
enter a number to check for: 1
number 1 found at position: [1, 15]

Run 2:
enter a list of numbers:
[1,2,3,3,4,5,6,7,8,5,23,43,23,1,0,3,0]
enter a number to check for: 2
number 2 found at position: [2]
COMPUTER SCIENCE
PRACTICAL EXERCISES 2023-2024
CLASS-XI
Ex.no:10
Date:
Creating a program to separate positive and negative
AIM:
To write a program to check a list and print positive and negative in
separate lists
SOURCE CODE:
n = eval(input("enter a list of numbers: "))
n1=[]
n2=[]
for i in n:
if i>=0:
if i not in n1:
n1.append(i)
else:
if i not in n2:
n2.append(i)
print("original list: ",n)
print("positive numbers: ",n1)
print("negative numbers: ",n2)
SAMPLE OUTPUT:

Run 1:
enter a list of numbers: [-5,4,-3,2,1,-2,0,5,6,7,8,-9,-10]
original list: [-5, 4, -3, 2, 1, -2, 0, 5, 6, 7, 8, -9, -10]
positive numbers: [4, 2, 1, 0, 5, 6, 7, 8]
negative numbers: [-5, -3, -2, -9, -10]

Run 2:

enter a list of numbers: [-10,3,4,5,-4,6,-6,0,1,2,3,-76,-


34,45]
original list: [-10, 3, 4, 5, -4, 6, -6, 0, 1, 2, 3, -76, -34, 45]
positive numbers: [3, 4, 5, 6, 0, 1, 2, 45]
negative numbers: [-10, -4, -6, -76, -34]

You might also like