while loop & nested loop
while loop & nested loop
INFORMATICS PRACTICES
CLASS XI
AISMV-----INFORMATICS PRACTICES-----XI
Recap of for loop
AISMV-----INFORMATICS PRACTICES-----XI
WAP to find sum of the series:
s = 1 + x + x2 + x3 + …… +xn
Control
While Loop statements in
a loop
AISMV-----INFORMATICS PRACTICES-----XI
while loop : Entry controlled Loop
Syntax:
while expression:
statement(s)
Necessary Conditions:
1) Initialization
2) Terminating Condition
3) Iteration
AISMV-----INFORMATICS PRACTICES-----XI
while loop allows multiple ways of iteration
Fixed number of steps
i=1
while(i<=10):
print(i)
i=i+1
AISMV-----INFORMATICS PRACTICES-----XI
While loop also works when number of steps are not known
Example
i=100
while(i>0):
print(i)
i=i/3
AISMV-----INFORMATICS PRACTICES-----XI
To count number of digits
n=int(input('Enter a number'))
c=0
while(n>0):
c=c+1
n=n//10
print(c)
AISMV-----INFORMATICS PRACTICES-----XI
While loop to do digit extraction
n=int(input('Enter a number'))
while(n>0):
print(n%10)
n=n//10
AISMV-----INFORMATICS PRACTICES-----XI
WAP to input a number and find the sum of
digits of the number.
n=int(input('Enter a number'))
s=0
while(n>0):
s=s+n%10
print(n%10)
n=n//10
print(“The sum of digits are = “,s)
AISMV-----INFORMATICS PRACTICES-----XI
To do: To reverse a number using digit
extraction
n=int(input('Enter a number'))
s=0
while(n>0):
s=s*10+n%10
n=n//10
print(s)
AISMV-----INFORMATICS PRACTICES-----XI
To do activity: Find if a number is
palindrome by using digit extraction
n=int(input('Enter a number'))
s=0
t=n
while(n>0):
s=s*10+n%10
n=n//10
print(s)
if(s==t):
print("Palindrome")
else:
print("Not a Palindrome")
AISMV-----INFORMATICS PRACTICES-----XI
Create menu driven program using while
loop
while(True):
print("1. Add")
print("2. Subtract")
print('3. Multiply')
print('4. Divide')
ch=int(input('Enter your choice'))
x=int(input('Enter the first number'))
y=int(input('Enter the second number'))
if(ch==1):
print(x+y)
elif(ch==2):
print(x-y)
elif(ch==3):
print(x*y)
elif(ch==4):
print(x/y)
else:
print('Invalid choice')
AISMV------INFORMATICS PRACTICES -----POOJA THAKUR
Write a program to display the menu for calculating the area of a circle, square, triangle and
sphere. Depending on the choice of the user, the inputs should be taken and the are area
should be displayed.
Sample Output:
Calculate Area:
1. Circle
2. Square
3. Triangle
4. Sphere
Enter your choice: 2
Enter the side: 4
The Area is 16
n=int(input('Enter a number'))
d=len(str(n))
t=n
s=0
while(n>0):
r=n%10
s=s+r**d
n=n//10
if(s==t):
print("Armstrong")
else:
print("Not an Armstrong number")
AISMV------INFORMATICS PRACTICES -----POOJA THAKUR
Practice Question
Neon number:
A number is said to be a Neon Number if the sum of
digits of the square of the number is equal to the
number itself. Example 9 is a Neon Number. 9*9=81 and
8+1=9.Hence it is a Neon Number.
while(True):
print('Hello')
i=15
while("i"):
print(i)
i=i-1
Find the output:
i=0
while i < 3:
print(i)
i += 1
else:
print(0)
x = "abcdef"
while i in x:
print(i, end=" ")
x = "abcdef"
i = "i"
while i in x:
print(i, end=" ")
❖Loop control statements change
execution from its normal sequence.
Loop Control
Statements ❖When execution leaves a scope, all
automatic objects that were created
in that scope are destroyed.
Loop Control Statements
Current Letter : P
break statement : Find the output
i=1
while True:
if i%2 == 0:
break
print(i)
i += 2
i=0
while i < 5:
print(i)
i += 1
if i == 3:
break
else:
print(0)
Using break statement in Programming
Finding a program to find if a number is prime
n=int(input('Enter a number'))
srt=int(n**0.5)
for i in range(2,srt+1):
if(n%i==0):
print("Not Prime")
break
else:
print('Prime')
In Python programming, the pass statement is a null statement.
for i in range(1,10):
if(i%2==0):
pass
else:
print(i)
Iterations in Python
(Nested Loops)
INFORMATICS PRACTICES
CLASS XI
Loop within a
loop is called
Nested Loop
Nested Loops
Used in Used in
generating creating
sequences patterns
#Output
11
Nested loops for a sequence 12
13
14
for i in range(1,5): 21
for j in range(1,5): 22
print(i,j) 23
24
31
32
33
34
41
42
43
44
To Generate a pattern
for i in range(1,5):
for j in range(1,5):
print('*',end=’’)
print()
#Output
****
****
****
****
Triangle of ‘*’
#Output
for i in range(5): *
for j in range(i+1): **
print('*',end=’’) ***
print() ****
*****
Exercise: Generate a pattern
#Output
1
for i in range(1,6): 12
for j in range(1,i+1): 123
print(j,end='') 1234
12345
print()
Exercise: Generate a pattern
#Output
1
for i in range(1,6): 22
for j in range(1,i+1): 333
print(i,end='') 4444
55555
print()
Exercise: Generate a pattern
#Output
54321
n=int(input('enter a number')) 5432
for i in range(1,n+1): 543
for j in range(n,i-1,-1): 54
print(j,end=’’) 5
print()
Exercise : Generate a pattern
y=input('Enter the alphabet')
y=ord(y) #Pattern
for i in range(65,y+1): A
for j in range(65,i+1): AB
print(chr(j),end=’’) ABC
print() ABCD
ABCDE
ABCDEF
ABCDEFG
Practice Question
#Output
Write a program to display the triangle of *
‘*’ given as output * *
* * *
* * * *
* * *
* *
*
Find the Output
x=0 #Output
for i in range(10): 9
for j in range(-1, -10, -1): 18
x += 1 27
36
print(x) 45
54
63
72
81
90
WAP to input the value of x and n and print
the sum of the series:
s=
x = int(input("Enter value of x "))
n = int(input("Enter value of n (for x**n) :"))
s=x
sign = +1
for a in range(2,n+1):
f=1
for i in range(1,a+1):
f *= i
term = ((x**a) * sign)/f
s += term
sign *= -1
print("Sum of first", n," terms : ",s)