Professional Documents
Culture Documents
Python code samples by summa irra sunni
Python code samples by summa irra sunni
Roll No:21
Section: S3 Ex.3 Iterative Statements
____________________________________________________________________
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
End
Code:
else:
print("Enter a valid grade")
---------------------------------------------------------------------------------------------------------------------
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
Algorithm/Pseudo-code:
Code:
num=int(input("Enter a number:"))
a=0
for i in range(1,num+1):
a+=i
print(a)
Output:
---------------------------------------------------------------------------------------------------------------------
Code:
Output:
---------------------------------------------------------------------------------------------------------------------
Qn3: Reverse the digits of a given number. Ex: Input: 1562, Output: 2651
Code:
Output:
---------------------------------------------------------------------------------------------------------------------
Qn4: Find the sum of the digits of a given number.
Code:
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:
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
Qn.8 Try atleast any 2 patterns among the 16 patterns as shown below:
Pattern 1:
Code:
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.