You are on page 1of 14

NATIONAL INSTITUTE OF TECHNOLOGY

ANDHRA PRADESH
Department of Computer Science and Engineering
Introduction to Algorithmic Thinking and Programming
Lab
Code : CS102
Academic Year : 2021-2022
1st Year B.Tech
2nd Semester
Name : Madhyala Bharadwaj
Roll Number : 9212
QUESTION 1:
Construct a python program to print the prime triplets between 10 and
153.Print the total number of prime triplets.
Note: A prime triplet is a set of three prime numbers of the form (p,p+2,p+6)
or (p,p+4,p+6) in which the smallest and the largest of three differ by 6.

CODE:
def primeornot(n):
if n==2:
return True
elif n==0 or n==1:
return False
else:
for i in range(2,n):
if n%i==0:
return False
return True

def primelist(start,last):
plist=[]
while start<(last+1):
if primeornot(start):
plist.append(start)
start=start+1
return plist

def triplets(start,last):
plist=primelist(start,last)
numtriplets=0
for a in plist:
if (a+2) in plist and (a+6) in plist:
print(a,a+2,a+6)
numtriplets=numtriplets+1
if (a+4) in plist and (a+6) in plist:
print(a,a+4,a+6)
numtriplets=numtriplets+1
print("Total number of prime triplets=",numtriplets)
triplets(int(input("Enter starting term: ")),int(input("Enter last term: ")))

Output :
Enter starting term: 10
Enter last term: 153
11 13 17
13 17 19
17 19 23
37 41 43
41 43 47
67 71 73
97 101 103
101 103 107
103 107 109
107 109 113
Total number of prime triplets= 10
QUESTION 2:
Write a python recursive program to reverse the digits of a number.
CODE :
def rev(n,r=0):
if n==0:
return r
else:
return rev(n//10,(r*10)+(n%10))
reversed_num=rev(int(input("Enter the number: ")))
print("The reversed number is",reversed_num)

Output :
Enter the number: 8745182
The reversed number is 2815478
QUESTION 3:
Construct a python program to print the chess board graphics using Turtle
programming.
Output :
import turtle
t=turtle.Turtle()
t.pensize(2)
for i in range (4):
t.fd(240)
t.lt(90)
n=8
for i in range (8):
t.up()
t.goto(0,30*i)
t.down
if i%2==0:
t.color('black')
t.begin_fill()
for k in range (4):
t.fd(30)
t.lt(90)
t.end_fill()
for k in range(3):
t.fd(60)
t.begin_fill()
for k in range (4):
t.fd(30)
t.lt(90)
t.end_fill()
else:
t.fd(30)
t.color('black')
t.begin_fill()
for j in range (4):
t.fd(30)
t.lt(90)
t.end_fill()
for k in range(3):
t.fd(60)
t.begin_fill()
for k in range (4):
t.fd(30)
t.lt(90)
t.end_fill()
Output :
QUESTION 4:
Write a python program to compute the frequencies of all letters appearing in a
string given as input.
CODE :
string = input(”Enter a string: ”)
dictionary = {}
for i in string :
if i in dictionary :
dictionary[i] = dictionary[i] +1
else :
dictionary[i] =1
for (key,values) in dictionary.items() :
print(key,”: ”,values)
Output :
Enter a string: computer science
c:3
o:1
m:1
p:1
u:1
t:1
e:3
r:1
:1
s:1
i:1
n:1
QUESTION 5:
Construct a python program to print the following pattern.
A
B C
D E F
G H I J
K L M N O
P Q R S T U
V W X Y Z
CODE :
n=7
p=65
for i in range (n):
for j in range (n-i):
print(" ",end="")
for k in range (i+1):
print(chr(p),end=" ")
if p<90:
p=p+1
else:
break
print()
Output :
A
B C
D E F
G H I J
K L M N O
P Q R S T U
V W X Y Z
QUESTION 6:
Write a program to generate a multiplication table from 11 to 20 .Write it to the different
files.
CODE :
for i in range (2,21):
with open (f'table{i}.txt','w') as table:
table.write(f'{i} TABLE\n')
with open (f'table{i}.txt','a') as t:
for j in range (1,11):
t.write(f'{i} x {j} = {i*j}\n')
for i in range (2,21):
with open(f'table{i}.txt','r') as f:
text=f.read()
print(text)
Output :
15 TABLE
15 x 1 = 15
15 x 2 = 30
15 x 3 = 45
15 x 4 = 60
15 x 5 = 75
15 x 6 = 90
15 x 7 = 105
15 x 8 = 120
15 x 9 = 135
15 x 10 = 150
16 TABLE
16 x 1 = 16
16 x 2 = 32
16 x 3 = 48
16 x 4 = 64
16 x 5 = 80
16 x 6 = 96
16 x 7 = 112
16 x 8 = 128
16 x 9 = 144
16 x 10 = 160

17 TABLE
17 x 1 = 17
17 x 2 = 34
17 x 3 = 51
17 x 4 = 68
17 x 5 = 85
17 x 6 = 102
17 x 7 = 119
17 x 8 = 136
17 x 9 = 153
17 x 10 = 170
18 TABLE
18 x 1 = 18
18 x 2 = 36
18 x 3 = 54
18 x 4 = 72
18 x 5 = 90
18 x 6 = 108
18 x 7 = 126
18 x 8 = 144
18 x 9 = 162
18 x 10 = 180

19 TABLE
19 x 1 = 19
19 x 2 = 38
19 x 3 = 57
19 x 4 = 76
19 x 5 = 95
19 x 6 = 114
19 x 7 = 133
19 x 8 = 152
19 x 9 = 171
19 x 10 = 190
20 TABLE
20 x 1 = 20
20 x 2 = 40
20 x 3 = 60
20 x 4 = 80
20 x 5 = 100
20 x 6 = 120
20 x 7 = 140
20 x 8 = 160
20 x 9 = 180
20 x 10 = 200

You might also like