You are on page 1of 8

Name – Naveen Agarwal

Roll no. – 39

Section – S8

Assignment 3: Problem-solving using Iterative statements


Aim : To apply computational thinking to devise solutions to the given problems and use the appropriate
looping constructs to solve problems by python code.

Q1. In a school, a total of X students are enrolled. Y students are in the elementary grade (below 2nd grade),
Z students are in middle school (grades 3 to 6), and the remaining students are in high school (grade 6 to
12). The fees for students are categorized as follows:( Consider total student strength as <=10)

● For high school students, the annual fee is Rs. XYZ.

● For middle school students, the annual fee is 30% less than the high school students pay

● For elementary grade students, the annual fee is 50% less than high school students pay

Calculate the student count in each category and annual fee for elementary grade students, middle school
students, high school students.

a. Identify the inputs required to solve the problem.

Ans. The input required to solve the problem are Total number of student (X)and Annual fee of the
school (F).

b. Devise a solution and represent the same using flowchart and pseudocode.

Ans. Algorithm –
Flowchart –

c. Develop a program to compute student count in each category and annual fee for elementary
grade students, middle school students, high school students (Hint: use appropriate looping
statements).

Ans. Code –
#problem1:calculate the total student in each category and annual fee of
a student
#input: total number of student and annual fee of the school .
#output: Annual fee after discount of different school and complete student count
of each category

X=int(input('enter the total number of student '))


F=int(input('enter the annual fee of the school '))
TF=0
ES=0
MS=0
HS=0
for i in range(1,X+1): #using for loop
G=int(input('enter the grade of the student'))
if G<=2: #using conditional statement
TF=F-(50/100)*F
ES=ES+1
print('the annual fees of the student is ',TF)
elif G>=3 and G<=6:
TF=F-(30/100)*F
MS=MS+1
print('the annual fees of the student is ',TF)
else :
TF=F
HS=HS+1
print('the annual fees of the studen is ',TF)
# display the output
print('the total number of elementary student is ',ES)
print('the total number of Middle school student is ',MS)
print('the total number of HIgh school student is ',HS)
#end

Outcome –
Q2. In an organization, there is a secret number locker to safeguard confidential files. The rule to
open the secret number locker follows a 2 step verification model.

Step-1. In step 1 The entered number is defined as the sum of n th power of each digit to a n digit
number is equal to that number. and Maximum chances to try the secret key are 3 times.

If the user successfully completes step 1, then move on to step 2.

Step-2: The user has to enter two numbers p and q and check if two numbers are prime pairs or
twin primes. Twin primes are two prime numbers that have a difference of 2 between them.

If the user successfully completed step 2, then “Locker opened successfully...” must be displayed.
The maximum chances to try the secret key in step 2 are two times.

Write the pseudocode for implementing the above scenario and print the result.

a. Identify the inputs required to solve the problem.

Ans. Password form the user (n) and two number (p, q).

b. Devise a solution and represent the same using flowchart and pseudocode.

Ans. Algorithm –
Flowchart –
c. Develop a program to open a secret locker based on a two-step verification process. (Hint:
use appropriate looping statements.)

Ans.

Code –
#Problem 2 : seting up a locker password
#input: Password and two number form user.
#output: opening a secret locker based on a two-step verification process.
#Step 1 : Verify the entered number
no_of_step=1
while no_of_step<=3:
n = input('enter the password ')
X = int(n)
Y = len(n)
temp = X
SP = 0 #sum of power
while temp>0: # checking the number = sum of power
z = temp%10 # taking only the last digit of number
SP = SP + z**Y # power of that number
temp = temp//10 # taking next number
if (SP == X):
S2 = True
print ('Step 1 is compelete , proceed to next step ')
break
else :
S2 = False
print("Step 1 Failed, Try Again")
no_of_step+=1

if no_of_step == 4:
print ('verification failed , try other time')

#Step 2 : verify two prime number as twin prime


if S2 :
no_of_step2 = 1
while no_of_step2 <= 2:
p = int(input('enter a prime number '))
q = int(input('enter another prime number '))
b = 0
d = 0
# checking whether p and q are prime number or not.
for a in range(1,p+1): # taking number from 1 to the given number
if (p%a) == 0 :
b = b + 1
for c in range(1,q+1):
if (q%c) == 0 :
d = d + 1
if b <= 2 and d <= 2: # prime number have 2 multiple
S = abs(q - p) # mode of (q-p) so that it always positive .
if S == 2 :
print ('verification compelete , locker unlocked ... ')
break
else :
print (f'Number are prime but not Twin prime , Try again (Attempts
remaining: {2 - no_of_step2}):')

else :
print("Wrong numbers, Try Again")
no_of_step2 += 1
if no_of_step2 == 3 :
print (' Step 2 failed , try again later :(' )
#End

Outcome

Ouput 1 –
Output 2 –

Lerning Outcome –

● I learn to apply computational thinking to devise solutions to the given problems.

● I learn to code python programs using appropriate looping constructs to solve problems.

You might also like