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

Name: Hemanth Santhosh

Roll No:21
Section: S3 Ex.3 Iterative Statements
____________________________________________________________________

Aim: To learn python programming using iterative statements by forming expressions


and statements involving reading and printing the data appropriately for the given
specification.

Qn.1 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.

Flowchart:
START

ENTER number of
students
Add fees of each student
based on grade

For i in false
range of
ENTER grade of
students no.
each student

true

Print total fees to be paid

End
Code:

x=int(input("Enter the number of students in school:"))


y=0
z=0
o=0
a_fee=80000
for i in range(x):
g=int(input("Enter the grade of student :"))
if g<=2:
y+=1
f1=a_fee*(1-(50/100))
print("Elementry grade")
print("annual fees :",f1)

elif g>=3 and g<=6:


z+=1
f2=a_fee*(1-(30/100))
print("middle school")
print("annual fees :",f2)

elif g>=6 and g<=12:


o+=1
f2=a_fee
print("high school")
print("annual fees :",f2)

else:
print("Enter a valid grade")

print("The number of students in elementry school is ",y)


print("The number of students in middle school is ",z)
print("The number of students in high school is ",o)
print("Total fees (elementry school):",y*a_fee*(1-(50/100)))
print("Total fees (middle school):",z*a_fee*(1-(30/100)))
print("Total fees (high school):",o*a_fee)
Output:

---------------------------------------------------------------------------------------------------------------------

Qn.2: 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 nth 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 and check if two numbers are prime pairs or prime
twins. Twin primes are two prime numbers that have a difference of 2 between them.
Twin primes are also known as prime pairs or prime twins. 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.

Code:

def prime(a):
for i in range(2,a):
if a%i==0:
return False
else:
return True

print("Step - 1")
print()
sum_num=0
for i in range(3):
n=input("Enter an Armstrong number :")
print()
l=len(n)
for j in n:
sum_num+=(int(j)**l)
if str(sum_num)!=n and i==2:
print("Maximum attempts reached. Locker remains locked")
print()
elif str(sum_num)==n:
print()
break
else:
print("Try again")
print()
sum_num=0
if str(sum_num)==n:
print("Step - 2")
print()
for r in range(2):
n1=int(input("Enter 1st twin prime :"))
n2=int(input("Enter 2nd twin prime :"))
if (prime(n1) and prime(n2)) and (n1-n2==2 or n1-n2==-2) :
print()
print("Locker opened successfully...")
break
elif (n1-n2!=2 or n1-n2!=-2) and r==1:
print()
print("Maximum attempts reached. Locker remains locked")
else:
print()
print("Try again, Enter correct value")
print()
Output:

---------------------------------------------------------------------------------------------------------------------

Additional Problems

Qn.1: Find the sum of N numbers. If N=5, then Sum=1+2+3+4+5=15

Algorithm/Pseudo-code:

1) Take a number ‘ n ‘ as input


2) A variable ‘a‘ is assigned as 0
3) Another variable ‘i’ is changing values with range as input value
4) ‘i’ is added to “a” and put in loop until it finishes the range
5) “a” is printed

Code:

num=int(input("Enter a number:"))
a=0
for i in range(1,num+1):
a+=i
print(a)
Output:

---------------------------------------------------------------------------------------------------------------------

Qn.2: Find the factorial of a given +ve integer.

Code:

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


a=1
for i in range(1,num+1):
a*=i
print(a)

Output:

---------------------------------------------------------------------------------------------------------------------
Qn3: Reverse the digits of a given number. Ex: Input: 1562, Output: 2651
Code:

num=int(input("Enter the number to be reversed:"))


r=0
a=0
while num>0:
a=num%10
r=r*10+a
num=num//10

print("The reversed nummber:",r)

Output:

---------------------------------------------------------------------------------------------------------------------
Qn4: Find the sum of the digits of a given number.

Code:

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


s=0

while num>0:
a=num%10
num=num//10
s+=a
print(num)

print(s)
Output:

---------------------------------------------------------------------------------------------------------------------

Qn5: Check whether a number is palindrome or not. Ex: 12321 is palindrome number.
The reversal of 12321 = 12321

Code:
num=input("Enter a number to be checked :")
a=num[::-1]
x=’’
for i in a:
x+=i
if x==num:
print("The number is a palindrome")
else:
print("The number is not a palindrome")

Output:
Qn 6. Print the Fibonacci series up to N numbers.

Code:

n=int(input("Enter range of fibonacci series:"))

a=0
b=1
print(a,b,end=" ")
for x in range(n-2):
a,b=b,a+b
print(b,end=' ')

Output:

---------------------------------------------------------------------------------------------------------------------
Qn 7. Take integer inputs from user until he/she presses ‘Q’. Print average and product
of all numbers.

Code:

n=0
s=0
p=1
count=0
while n==0:
x=input("Enter the value :")
count+=1
if x!='Q':
s+=int(x)
p*=int(x)
elif x=="Q":
break

print("The average of the above values:",s/(count-1))


print("The product of the above values:",p)
Output:

Qn.8 Try atleast any 2 patterns among the 16 patterns as shown below:

Pattern 1:

Code:

n=int(input("Enter the number of rows:"))


a=1
for i in range(n-1,-1,-1):
print(" "*i,end=" ")
print("*"*a)
a+=2

Output:
Pattern 2:

Code:
n=int(input("Enter the number stars needed in the middle rows :"))
for i in range(1,n+1):
for j in range(i):
print("*",end=" ")
print()
for k in range(n-1,0,-1):
for l in range(k):
print("*",end=" ")
print()

Output:

Learning outcome:
1. Reading inputs / Printing the result
2. Using appropriate datatypes for the given input
3. Variable assignment
4. Converting the formula into python expressions
5. Using if else statements
6. Using looping statements

Result:
Thus I learned to implement a simple problems in Python and solve the same using
alternate statements.

You might also like