You are on page 1of 46

Q1 Pengram

Pengrams are words or sentences containing every letter of the English alphabet at
the most once. Write an algorithm and a subsequent Python code to check whether
a string is a pengram or not. Write a function to check if a given string is a pengram.
For example, “He is at work” is a pengram. Since every letter of the english alphabet
occurs at the most once

Input format

First line contains the string to be checked

Output Format

Print Pengram or Not pengram

INPUT
First line contains the string to be checked

PROCESSING
In the string check whether each letter

does not occur more than one time.

If it does print Not pengram

else print Pengram

OUTPUT
Print Pengram or Not pengram

PSEUDOCODE
1.Start

2.

s=input()

for i in s:

cnt=0

for j in s:

if i==j and i!=' ':

cnt+=1

if cnt>1:

print("Not pengram")
break

if cnt==1:

print("Pengram")

3.End

CODE
s=input()

for i in s:

cnt=0

for j in s:

if i==j and i!=' ':

cnt+=1

if cnt>1:

print("Not pengram")

break

if cnt==1:

print("Pengram")

Q2 Oddophic Numbers
Given ‘n’ integers, write an algorithm and the subsequent Python code to print all
numbers that are oddophic to the first number. Two numbers with non-distinct
(numbers in which digits get repeated) digits are said to be oddophic if they have the
same number of digits in it and the sets of positions having the same digits contains
only odd positions. Positions of digits are numbered from left to right starting from 1.
For example:

12161 is oddophic to 93968 .


Both the numbers are of length five. In 12161, positions 1, 3 and 5 have the same
digit 1. Hence the set of positions having the same digit are {1,3,5}. Similarly , for the
number 93968, the set of positions having the same digit is {1,3}. In both the
numbers, the sets of positions having the same digit contains only odd positions.
1232 is not oddophic to 2342 because set of positions having same digits for 1232 is
{{2, 4}} and for 2342 is {{1, 4}}.
12 is not oddophic to 10, since the digits are distinct. No digit gets repeated in 12
and 10.
Write a function to check whether two numbers are oddophic. If none of the numbers
are oddophic then print ‘No oddophic’.

Input Format

First line contains the number of elements, n

Next ‘n’ line contains the numbers

Output Format

Print first number in the first line

Next few lines contain the numbers that are oddophic to first number.

If the none of the numbers are not oddophic, then Print “No oddophic”

INPUT
First line contains the number of elements, n

Next ‘n’ line contains the numbers

PROCESSING
for i in range(0,len(str1)):

for j in range(i+1,len(str1)):

if(str1[i]==str1[j]):

if((i+1)%2==0 or (j+1)%2==0):

return -1

OUTPUT
Print first number in the first line

Next few lines contain the numbers that are oddophic to first number.

If the none of the numbers are not oddophic, then Print “No oddophic”

PSEUDOCODE
Start

flag=1
def odd(str1,str2,wd):

count=0

if((len(set(str1))==len(str1)) or (len(set(str1))==len(str2))):

return -1

if(len(str1)!=len(str2)):

return -1

for i in range(0,len(str1)):

for j in range(i+1,len(str1)):

if(str1[i]==str1[j]):

if((i+1)%2==0 or (j+1)%2==0):

return -1

for i in range(0,len(str2)):

for j in range(i+1,len(str2)):

if(str2[i]==str2[j]):

if((i+1)%2==0 or (j+1)%2==0):

return -1

if(wd==0):

print(str1)

print(str2)

return 0

n=int(input())

a=input()

ad=0

count=0

for i in range(1,n):

w=odd(a,input(),count)

if(w==0):

ad=ad+1
count=1

if(ad==0):

print("No oddophic")

Stop

CODE
flag=1

def odd(str1,str2,wd):

count=0

if((len(set(str1))==len(str1)) or (len(set(str1))==len(str2))):

return -1

if(len(str1)!=len(str2)):

return -1

for i in range(0,len(str1)):

for j in range(i+1,len(str1)):

if(str1[i]==str1[j]):

if((i+1)%2==0 or (j+1)%2==0):

return -1

for i in range(0,len(str2)):

for j in range(i+1,len(str2)):

if(str2[i]==str2[j]):

if((i+1)%2==0 or (j+1)%2==0):

return -1

if(wd==0):

print(str1)

print(str2)

return 0

n=int(input())
a=input()

ad=0

count=0

for i in range(1,n):

w=odd(a,input(),count)

if(w==0):

ad=ad+1

count=1

if(ad==0):

print("No oddophic")

Q3 Heterosquare Numbers
Heterosquare numbers are the numbers having “n” digits such that the last n digits of
the square of the number will not be the number itself. .Write an algorithm and the
subsequent Python code to check if the given number is a Heterosquare number.
Write a function to find the square of a given number. For example, 22 is a 2 digit
heterosquare number with a square of 484 and 111 with its square 12321, is a 3 digit
heterosquare number.

Input Format

First line contains the number to be checked

Output Format

Print Heterosquare or Not Heterosquare


INPUT
First line contains the number to be checked

PROCESSING
square the number with n digits and check whether the last n digits are same as the
number given.

OUTPUT
Print Heterosquare or Not Heterosquare

PSEUDOCODE
1. Start

2.n=m*m

m=str(m)

n=str(n)

if n[len(n)-len(m):len(n)]!=m:

print("Heterosquare")

else:

print("Not heterosquare")

3.End

CODE
m=int(input())

n=m*m

m=str(m)

n=str(n)

if n[len(n)-len(m):len(n)]!=m:

print("Heterosquare")

else:

print("Not Heterosquare")
Q4 Diff-adam number
A number ‘n’ is said to be a Diffadam number if the absolute value of the difference
between the number ‘n’ and the reverse of ‘n’ is zero. For example, 121 is Diffadam
number. Reverse of a number is the number got by writing the digits of the number in
the reverse order (from right to left). Reverse of 132 is 231.

Write an Algorithm and the subsequent Python code to check whether the given
number is a Diffadam number or not.

Write a function to reverse a number

Input Format:

The first line will contain the number.

Output Format:

Print Diffadam number or Not a Diffadam number

INPUT
The first line will contain the number.

PROCESSING
for all the number given check whether each

of them is oddophic with first number or not.

If not print Not oddophic

else print Oddophic

OUTPUT
Print Diffadam number or Not a Diffadam number

PSEUDOCODE
1.Start

2.

def reverse(string):

if len(string) == 0:

return string

else:

return reverse(string[1:]) + string[0]

a=input()

if (int(a)-int(reverse(a))==0):

print("Diffadam number")
else:

print("Not a Diffadam number")

3.End

CODE
def reverse(string):

if len(string) == 0:

return string

else:

return reverse(string[1:]) + string[0]

a=input()

if (int(a)-int(reverse(a))==0):

print("Diffadam number")

else:

print("Not a Diffadam number")

Q5 Elizabeth’s exercise
Elizabeth visits her friend Andrew and then returns home by the same route. She
always walks 4 kilometers per hour (km/h) when walking uphill, 8 km/h when walking
downhill, and 6 km/h when walking on level ground. Suppose the path from
Elizabeth’s home to Andrew’s home consists of ‘x’ meter in the level ground, ‘y’
meter in the uphill, ‘z’ meter in the downhill and Elizabeth starts from home by 6 a.m.
Write an algorithm and the subsequent Python code to determine when Elizabeth will
reach Andrew’s home and when she will reach her home back if she spends ‘m1’
minutes in Andrew’s home. For example, if x is 1000, ‘y’ is 500, ‘z’ is 300 and m1 is
’30’ minutes, then Elizabeth takes 38 min to reach Andrew’s home so she will reach
Andrew’s home by 6 hour 21 min. Elizabeth will take 19 min to walk from Andrew’s
home to her home and time when will reach her home back is 7 hour 10 min.

The hour can be expressed in 12-hour format (not 24-hour format). The minutes
elapsed should be rounded down to the nearest integer.
Input Format

First line contains the distance ‘x’ in level ground

Next line contains the distance ‘y’ in uphill

Next line contains the distance ‘z’ in downhill

Next line contains the value for ‘ml’ minutes spent at Andrew’s home

Output Format

Print time by which Elizabeth will reach Andrew’s home. Print hours and minutes
separated by a space

Print time by which Elizabeth will reach back her home. Print hours and minutes
separated by a space

INPUT
x- Distance on level ground
y- Distance uphill
z- Distance downhill
m- Minutes spent at Andrew’s home

PROCESSING
Using the basic time speed distance formula, we calculate the time spent in travelling
to and fro between the house and the destination.

OUTPUT
Time at which she reaches Andrew’s home

Time at which she reaches back to her home

PSEUDOCODE
BEGIN

Read x, y, z and m
t=(x/6+y/8+z/4)*(3/50)
tx=(x/6+y/4+z/8)*(3/50)
print(int(6+(t//60)),int((t)%60))
print(int(6+((t+tx+m)//60)),(int(t)+int(tx)+m)%60)

END
Question 1 (Letter Identification)
Given three words, write an algorithm and the subsequent Python code to identify
the following letters:

1. Letters common to all the three words


2. Letters in first two words but not in third word
3. Letters in first word but not in second and third word
4. Letters in all the three words
For example, if the words are apple, camel, element then letters in common to all the
three words – i, e

Letters in first two words but not in third word – a

Letters in first word but not in second and third word – p

Letters in all the three words – a, p, p, l, e, c, m, n, t

Hint: Use sets in Python. While reading input, use rstrip() function to remove extra
spaces

Input Format

First line contains word1

Second line contains word2

Third line contains word3

Output Format

List of letters common to all the three words in lexicographical order

List of letters common to first two words but not in third word in lexicographical order
List of letters in first word but not in second or third word in lexicographical order

List of all letters in the three words in lexicographical order

INPUT
First line contains word1

Second line contains word2

Third line contains word3


PROCESSING
Using basic set operations, we find the output for the given three conditions.
OUTPUT
List of letters common to all the three words in lexicographical order

List of letters common to first two words but not in third word in lexicographical order

List of letters in first word but not in second or third word in lexicographical order

List of all letters in the three words in lexicographical order

Solution
s1=set(input().rstrip())

s2=set(input().rstrip())

s3=set(input().rstrip())

s=list(s1&s2&s3)

s.sort()

print(s)

s=list((s1&s2)-s3)

s.sort()

print(s)

s=list(s1-(s2|s3))

s.sort()

print(s)

s=list(s1|s2|s3)

s.sort()

print(s)
Question 2 (Minimum distance points)
Given ‘n’ points in an X-Y plane , write an algorithm and the subsequent Python code
to determine the pair of points that are closer. Distance between two points (x1, y1)
and (x2, y2) is determined using the formula.

Consider only two decimal places of distance for comparison. When there are
multiple points with the same minimum distance print all points.

Input Format

Number of points

x coordinate of point1

y coordinate of point1

x coordinate of point2

y coordinate of point2

Output format

Point1 and Point2 that have minimum distance between them

INPUT
n, number of points
x coordinates of n points
y coordinates of n points
PROCESSING
Using a pair of nested for loops, we find the distance between all different points and
find the points with the least distance between them.

OUTPUT
Points with least seperation.

Solution
n=int(input())

l=[]

min=999

for i in range(n) :

x=int(input())
y=int(input())

l.append((x,y))

for i in range(0,n-1) :

for j in range(i+1,n) :

x1,y1=l[i]

x2,y2=l[j]

d=(((x2-x1)**2)+((y2-y1)**2))**(0.5)

if d < min :

min=d

m=[]

m.append((x1,y1))

m.append((x2,y2))

elif d==min :

m.append((x1,y1))

m.append((x2,y2))

for i in m :

print(i)

Question 3 (Count Words)


Write an algorithm and write the Python code to count the number of unique words in
a given passage. The paragraph may contain words with special characters such as
‘!’, ‘?’, ‘.’, ‘,’ , ‘:’ and ‘;’ and digits are not permitted. Special character must occur only
at the end of a word in a passage that is Hello World! is valid but Hello !World or
Hello Wor!ld is invalid. No two special character occur together. Print ‘Invalid input’
for such cases. Count words without special characters. Counting must be case
insensitive. Print words in lower case and in sorted order. For example, given a
passage ‘Programming is a skill in demand! Your programming skills are bankable
assets.’ The output should be

{‘a’: 1,

‘are’: 1,

‘assets’: 1,
‘bankable’: 1,

‘demand’: 1,

‘in’: 1,

‘is’: 1,

‘programming’: 2,

‘skill’: 1,

‘skills’: 1,

‘your’: 1}

[Hint: use pprint function for printing dictionary in sorted order.

Syntax for pprint is

pprint(dictionary name)

Include the line “from pprint import pprint” in the top of your program]

Input format:

enter a paragraph

Output format:

Dictionary of words in the passage and their count

Boundary conditions:

One paragraph is entered at a strech

INPUT
enter a paragraph
PROCESSING
OUTPUT
Dictionary of words in the passage and their count

Solution
import pprint

w=str(input())

l=w.split()

t=[]

d={}
f=1

for i in l :

k=''

for j in i :

k+=j.lower()

t.append(k)

for i in t :

if i.isalpha() :

if i in d :

d[i]+=1

else :

d[i]=1

elif i[:len(i)-1].isalpha() :

i=i[:len(i)-1]

if i in d :

d[i]+=1

else :

d[i]=1

else :

f=0

break

if f==1 :

pprint.pprint(d)

else :

print('Invalid input')

Question 4 (Word histogram)


Histogram is a graphical representation drawn based on the frequency of occurrence
of things. Histogram for a word is drawn based on the number of occurrences of
each character in the word. An inverted dictionary is used to maintain the list of
characters that has same frequency. Write an algorithm and the subsequent Python
code to compute and print the frequency of occurrence of character as a dictionary
(ch:count). Make the entire process to be case insensitive. For example, if the input
word is ‘parrot’, then the dictionary with characters and frequency of occurrence is

{‘a’: 1, ‘o’: 1, ‘p’: 1, ‘r’: 2, ‘t’: 1}

and the inverted dictionary is

{1: [‘a’, ‘o’, ‘p’, ‘t’], 2: [‘r’]}

Print dictionary in sorted order by key. Sort the values of each key in the inverted
dictionary.

[Hint: use pprint function for printing dictionary in sorted order.

Syntax for pprint is

pprint(dictionary name)

Include the line “from pprint import pprint” in the top of your program

Check for boundary conditions and print ‘Invalid input’ if conditions are not met.

Input Format:

First line contains the input word

Output Format:

Dictionary of characters in the word as keys and count as values in sorted order by
key.

Inverted dictionary in sorted order by key.

Boundary Conditions:

Given word should only contain alphabets

INPUT
First line contains the input word
PROCESSING
We find the frequency of each character in the given string using a pair of nested for
loops.
OUTPUT
Dictionary of characters in the word as keys and count as values in sorted order by
key.
Solution
import pprint

d={}

f={}

w=input()

if w.isalpha() :

for i in w :

if i.isupper() :

i=i.lower()

if i in d :

d[i]+=1

else :

d[i]=1

for i in d :

if d[i] in f :

f[d[i]].append(i)

else :

f[d[i]]=[i]

for i in f :

f[i].sort()

pprint.pprint(d)

pprint.pprint(f)

else :

print('Invalid input')

Question 5 (Rook and a Queen)


Given the position of a Rook and a queen in a chess board (8X8 board), write an
algorithm and the subsequent Python code to determine the common positions
where both rook and queen can be placed in the next move. Rook can move through
any number of cells, either horizontally or vertically. Queens can move through any
number of cells, either horizontally, vertically or diagonally. Each cell in the chess
board may be represented as a 2-tuple (row,col). For example, if the current position
of the rook is (3,1) then the next possible position of the rook may be either in the
same column {(2,1),(1,1),(4,1),(5,1),(6,1),(7,1),(8,1)} or in the same row
{(3,2),(3,3),(3,4),(3,5),(3,6),(3,7),(3,8)}. If the queen is in the position (5,3) then it can
be placed in the same row {(5,1),(5,2),(5,4),(5,5),(5,6),(5,7),(5,8)} or same column
{(1,3),(2,3),(3,3),(4,3),(6,3),(7,3),(8,3)} or along the diagonal of the current position
{(6,4),(7,5),(8,6),(4,2),(5,1),(6,2),(7,1),(4,4),(3,5),(2,6),(1,7)}. Then the common cells
for next move are {(3,3), (5,1), (7,1)}.

The output is a set of common board positions where both queen and rook can be
placed. The positions must be printed in sorted order, sort it by row. When rows are
same, sort it by column.

(Hint: Use built-in function to sort the values)

Input Format

Row position of rook

Column position of rook

Row position of queen

Column position of queen

Output Format

Common position1

Common position2

INPUT
Row and column position of rook
Row and column position of queen

PROCESSING
We check all the possible next moves of the queen and the the rook and sort and
display them.

OUTPUT
Common positions of intersection.

Solution
r={0}

r2={0}

xr=int(input())
yr=int(input())

xq=int(input())

yq=int(input())

s1=xq+yq

a=xq-yq

b=yq-xq

for i in range(1,9) :

r2.add((xr,i))

r2.add((i,yr))

r2=r2-r

r2=r2-{(xr,yr)}

q={0}

q2={0}

for i in range(1,9) :

for j in range(1,9) :

if i+j==s1 :

q2.add((i,j))

elif xq>yq :

if i-j==a :

q2.add((i,j))

elif yq>xq :

if j-i==b :

q2.add((i,j))

for i in range(1,9) :

q2.add((xq,i))

q2.add((i,yq))

q2=q2-q

q2=q2-{(xq,yq)}

a=r2&q2
c=list(a)

c.sort()

for i in c :

print(i)

1) CSE1701 FC for three digit Armstrong numbers (Id-1760)


Design an algorithm and draw a flow chart to check the given number is Armstrong
number of three digits or not. A number is said to be Armstrong if summation of cube
of digits in a three digit number is equal to the number. Check for boundary
conditions, if the value entered is outside boundary conditions then enter ‘Invalid
input’

Input Format:
A number ‘n’
Output Format:
Print Armstrong or Not armstrong
Boundary Conditions:
n>=100 and <=999
#note: Type the following statement to pass the code successfully.
print(‘Hello’)

Answer:
Input for the problem
A number n

Processing involved
Using modulus and integer division we extract individual digits and calculate the
summation of their cubes and compare with the original input.

Output for the problem


Armstrong (if number is armstrong)
or Not armstrong (if number is not armstrong)

Solution alternatives
We could have alternatively have written a program for any digit number.

Pseudocode (Algorithm)
Step 1: Start
Step 2: Input n
Step 3: Compute y=(n%10)^3+((n//10)%10)^3+(n//100)^3
Step 4: If y equals n
Step 4.1 : Display “Armstrong”
Step 5: Else
Step 5.1 Display “Not Armstrong”
Step 6: Stop
Flowchart:

2) CSE1701 Milk in Booth (Id-1763)


Every day morning and evening milk is brought from ‘n’ farms to a milk booth for
sales. Given the amount of milk from ‘n’ farms, write an algorithm to compute total
quantity of milk in the booth.
For example, if milk comes from 3 farms in quantities 2 litres 300ml, 3 litres 700ml
and 4 litres 600ml then the total quantity of milk in booth is 10litre 600ml.

Input Format:
First input is number of farms ‘n’
Next ‘n’ input is the amount of milk from ‘n’ farms

Output Format
Total quantity of milk in booth

Boundary Condition:
All input > 0
#note: Type the following statement to pass the code successfully.
print(‘Hello’)

Input for the problem


Number of farms, n
Amount of milk from n farms

Processing involved
We convert the total amount of milk from n farms into ml and add.
We then use modulus by 1000 to find the resulting ml and integer division by 1000 to
find the resulting litres

Output for the problem


Total quantity of milk in booth

Solution alternatives
We could alternatively have added litres seperately and milliliters seperately and
then adjusted the excess ml into litres.

Pseudocode
Step 1: Start
Step 2: Input n
Step 3: Assign sum=0
Step 4: Assign i=0
Step 5: If(i<n)
Step 5.1 sum=sum+input() in ml
Step 5.2 i=i+1
Step 5.3 go to step 5
Step 6: Display( sum//1000 litres and sum%1000 ml)
Step 7: Stop

Flowchart:
3) CSE1701 AND gate (Id-1762)
Devise an algorithm and draw a flowchart to simulate the working of an AND gate.
AND gate takes two bits as input and output a bit as shown in the following table.
Check for validity of input and print ‘Invalid input’ when user gives out of boundary
values.

X Y Output

0 0 0

0 1 0

1 0 0

1 1 1

Input Format:
Read input for ‘X’ as,
0
0
1
1

Read input for ‘Y’ as,

0
1
0
1

Output Format:
Print output as,

0
0
0
1

Boundary Condition:
X,Y should be either 0 or 1

#note: Type the following statement to pass the code successfully.

print(‘Hello’)

Input for the problem


One bit as x
Another bit as y

Processing involved
We multiply the two bits to get the output

Output for the problem


Solution bit

Solution alternatives
This is the best solution.We could have otherwise derived else if cases from the table
given in the question and found the solution.
Pseudocode
Step 1: Start
Step 2: Input x and y
Step 3: Check if x and y are valid
Step 4: Compute z=x*y
Step 5: Display y
Step 6: Stop

Flowchart:
4) CSE1701 Minimum marks (Id-1759)
Arjun, amith and sharma are friends. Given their marks in Maths, design an
algorithm and draw a flowchart to find minimum of their marks.

Input Format:
Get 3 inputs as marks for students.

Output Format:
Print the name of the student, who secured minimum marks in maths and marks
secured

Boundary Conditions:
Marks for each student to be >=0 and <=100

#note: Type the following statement to pass the code successfully.

print(‘Hello’)

Input for the problem


Marks of three students m1, m2, m3
Processing involved
Compare the marks using relational operators and find the minimum marks.

Output for the problem


Name of student who scored the least marks

Solution alternatives
None

Pseudocode
Step 1: Start
Step 2: Input m1, m2 , m3
Step 3: if(m1>m3 and m2>m3)
Step 3.1 assign min=m3
Step 4: otherwise if(m1>m2 and m3>m2)
Step 4.1 assign min=m2
Step 5: otherwise
Step 5.1 assign min=m1
Step 6: Display min
Step 7: Stop

Flowchart:
5) CSE1701 Health drink Festival Bonanza (Id-1761
A health drink company, gives a festival offer to its retail customers. The health drink
is packed in 1 kg pack. For every 5, one kg pack purchase, one 1kg pack will be
free. (i.e. buy 5 get 1 free). Given the number of health drink packets purchased,
design an algorithm and draw a flowchart to determine the total number of packets
that the customer will get
Example: If one small retail shop owner buys 125 one kg health drink pack, he will
get 150 one kg packs.

Explanation : Because for the 125 packs he purchased, he gets 25 packs free.

Input format:
Number of health drink packets purchased

Output format:
Number of packets delivered

Boundary conditions :
0< complan pack <= 500

#note: Type the following statement to pass the code successfully.


print(‘Hello’)

Input for the problem


Number of health drink packets purchased, n

Processing involved
We calculate the number of packets the customer gets as n + n/5

Output for the problem


The number of packets delivered

Solution alternatives
None

Pseudocode
Step 1: Start
Step 2: Input n
Step 3: Compute a=n+n/5
Step 4: Display a
Step 5: Stop

Flowchart:

1) Water in a Dam
A city has a dam of capacity ‘x’ litres, water comes to the dam from ‘n’ places. Given
the value of ‘n’ and the quantity of water (in litres and millilitres) that comes from ‘n’
places, Write an algorithm and the corresponding Python code to determine the total
amount of water in the dam. Assume that the total quantity of water in the dam, will
be always less than the capacity of the dam. For example, if there are three places
from which water comes to the dam and the water from place 1 is 2 litres 500 ml,
water from second place is 3 litres 400 ml and water from third place is 1 litre 700 ml
then the total quantity of water in dam will be 7 litres 600 ml.
Input Format
Number of places from where water comes, n

Number of litres of water from place-1

Number of millilitres of water from place-1


Number of litres of water from place-2

Number of millilitres of water from place-2

Number of litres of water from plac- n

Number of millilitres of water from place- n

Output Format
Total litres of water in the dam

Total millilitres of water in the dam

Input for the problem:


No of places, n
No of litres from n places
No of millilitres from n places
Processing involved:
Millilitres=total millilitres%1000
Litres=total litres+total litres//1000

Output for the problem:


Total millilitres of water in the dam
Solution alternatives:
None
Pseudocode:
Step 1: Start

Step 2: Input n (No. of places)

Step 3: Compute totalml=sum of litres from n places

Step 4: Compute totallitres=sum of litres from n places

Step 5: Final ml= totalml%1000

Step 6: Final litres=totallitres+totalml//1000

Step 7: Display final litres and final millilitres

Step 8: Stop

CODE:
n=int(input())

l=0

ml=0
for i in range(0,n):

l=l+int(input())

ml=ml+int(input())

l=l+ml//1000

ml=ml%1000

print(l)

print(ml)

Flowchart:
2) CoPrime
Given two numbers ‘m’ and ‘n’, design an algorithm and write the Python code to
check if they are relatively prime. Two integers ‘a’ and ‘b’ are said to be relatively
prime, mutually prime, or coprime, if the only positive integer that divides both of
them is 1. For example, 14 and 15 are coprime since the factors of 14 are 1, 2, 7, &
14 and factors of 15 are 1, 3, 5, & 15 and there is no common factor other than 1.
(Use only conditional and iterational statements for designing the algorithm and
implementation).

Input Format
Number m

Number n

Output Format
Print either Coprime or Not coprime

Input for the problem:


A number, m
A number, n

Processing involved:
Using iterative statements, we check for any number that divides both m and n

Output for the problem:


Coprime/Not coprime

Solution alternatives:
We could have alternatively found factors of both the numbers and compared them.

Pseudocode:
Step 1: Start

Step 2: Input m,n

Step 3: Assign i=2, f=0

Step 4: If(i<m)

Step 4.1: If(m%i==0 and n%i==0)

Step 4.1.1 f=1

Step 4.2 i=i+1

Step 5: If(f==0)

Step 5.1: Display Coprime


Step 6: Else

Step 6.1: Display Not coprime

Step 7: Stop

Flowchart:
CODE:
m=int(input())

n=int(input())

t=max(m,n)

flag=0

for i in range(2,t+1):

if(m%i==0 and n%i==0):

flag=1

if(flag==1):

print("Not coprime")

else:

print("Coprime")

Better alternative:
m=int(input())

n=int(input())

flag=0

for i in range(2,int(m**.5)+1):

if(m%i==0 and n%i==0):

flag=1

if(flag==1):

print("Not coprime")

else:

print("Coprime")

3) Pattern
Input for the problem:
A number, n

Processing involved:
Using an iterational statement we print appropriate “**” in each line

Output for the problem:


Pattern

Solution alternatives:
None

Pseudocode:
Step 1: Start

Step 1: Input n

Step 2: Assign i=0

Step 3: If(i<n)

Step 3.1: Display(i*"**")

Step 3.2 i=i+1

Step 3.3 Go to Step 3

Step 4: Stop

Flowchart:
CODE:
n=int(input())

for i in range(1,n+1):

print('**'*i)

4) Age
Input for the problem:
Number of days, n
Processing involved:
Seconds=n*24*60*60

Output for the problem:


No of seconds

Solution alternatives:
None

Pseudocode:
Step 1: Start

Step 2: Input n

Step 3: Display (n*24*60*60)

Step 4: Stop

Flowchart:

CODE:
a=int(input())

if(a>0 and a<28):

print(a*24*60*60)
else:

print("Invalid input")

5) Roman
Input for the problem:
A roman letter

Processing involved:
Using conditional statements, we find and display the value of the letter.

Output for the problem:


Value equivalent to the letter

Solution alternatives:
None

Pseudocode:
Step 1: Start

Step 2: a=input()

Step 3: If(a=='I'):

Step 3.1: print("1")

Step 4: Else if(a=='V'):

Step 4.1: print("5")

Step 5: Else if(a=='X'):

Step 5.1: print("10")

Step 6: Else if(a=='L'):

Step 6.1: print("50")

Step 7: Else if(a=='C'):

Step 7.1: print("100")

Step 8: Else if(a=='D'):

Step 8.1: print("500")

Step 9: Else if(a=='M'):


Step 9.1: print("1000")

Step 10: Else:

Step 10.1: print("Enter a roman numeral")

Step 11: Stop

Flowchart:
CODE:
a=input()

if(a=='I'):

print("1")

elif(a=='V'):

print("5")

elif(a=='X'):

print("10")

elif(a=='L'):

print("50")

elif(a=='C'):

print("100")

elif(a=='D'):

print("500")

elif(a=='M'):

print("1000")

else:

print("Enter a roman numeral")

6)Digit Factors in a Number


Given a number ‘n’, design an algorithm and write the Python program to print the
digits of ‘n’ that divides ‘n’. Print the digits in reverse order of their appearance in the
number ‘n’. For example, if n is 122 then print 2, 2, 1. Use only conditional and
iterative statements to write the code. If none of the digits divide the number, then
print ‘No factors’
Input Format
A number, n

Output Format
Digits in the number ‘n’ that divides ‘n’ in reverse order
Input for the problem:
A number, n

Processing involved:
We seperate the digits of the number using the modulus operator and check if it
divides the original number.

Output for the problem:


Digit factors of the number.

Solution alternatives:
None

Pseudocode:
Step 1: Start

Step 2: Input n

Step 3: Assign n1=n

Step 4: If(n1>0)

Step 4.1: If(n%(n1%10)==0)

Step 4.1.1: Display (n1%10)

Step 4.2: n1=n1//10

Step 4.3: Go to step 4

Step 5: Stop

Flowchart:
CODE:
a=int(input())

a1=a

flag=0

while(a1>0):

if(a%(a1%10)==0):
print(a1%10)

flag=1

a1=a1//10

if(flag==0):

print("No factors")

You might also like