Class 11 - Presentation # 5 - Iterative Statements - While Loop
Class 11 - Presentation # 5 - Iterative Statements - While Loop
XI
Introduction
Iteration means to do something repeatedly. Iteration is also called loop.
2
XI
Types of Loops
Loops
for while
3
XI
while loop
while is a loop used for repeated execution as long as a specified expression
Syntax : Statements 1
[else:
Statements 2]
4
XI
while loop
While loop with else clause While loop without else clause
false false
Out of loop
5
XI
Loop Control Elements
Every while loop has its elements that control and govern its execution. A while
6
XI
Loop Control Elements
7
XI
while loop
Statements
…..
…..
Update expression
8
XI
Some important points:
In a while loop, the loop control variable should be initialized before the
●
The loop variable must be updated inside the body of the loop in a way
●
that after sometime the test condition becomes false otherwise the loop
will become an endless loop or infinite loop.
● While loop is also called entry-controlled loop as the entry into the
loop is controlled by testing condition.
9
XI
Example: Code to print a message 5 times on
screen Test
Initialisation
count=1
while count<=5: Body of loop
print("Learn Python")
count=count+1
Update expression
10
XI
Example : Code to print first 10 natural
numbers
11
XI
Question 1
Write a program to print all even numbers from 1 to 10 and find their sum too.
12
XI
Question 2
Write a program to print table of number n.
13
XI
Question 3
Write a program input a number and then find the factorial of a number.
14
XI
Programs
1. Print all the natural numbers from m to n.
16
XI
Question 2
Write a program to print all the natural numbers from m to 1.(Reverse Order)
17
XI
Question 3
Write a program to input a number n and then print all the odd numbers from 1 to m
18
XI
Question 4
Write a program to input a number n and then print all its factors.
19
XI
Question 5
Write a program to input a number n and then print the sum of all its factors.
21
XI
Explanation :
I 9 9 230
II 0 90 23
III 3 903 2
IV 2 9032 0
Enter a number:
num = int(input("Enter a number: "))
4224
rev = 0
4224 is a palindrome
t = num
while t > 0:
digit = t % 10
rev=rev*10+digit
t //= 10
if num == rev:
print(num,"is a Palindrome")
else:
print(num,"is not a palindrome") 23
XI
Question 6
Write a program to input a number and then print if the number is an armstrong
number or number
not . is a number that is equal to the sum of cubes of its digits. Eg:(153 =
s a word,
num = number
int(input("Enter a number: ")) Enter a number: 153
sum = 0 153 is an Amrstrong
t = num number
while t > 0:
digit = t % 10
sum += digit ** 3
t //= 10
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
24
XI
Question 7
Write a program to print fibonacci series till value n
s a word, number
num = int(input("Enter a number: ")) Enter a number: 10
a=0 0,1,1,2,3,5,8
b=1
print(a,end=',')
print(b,end=’,’)
c=a+b
while c <=num:
print(c,end=',')
a=b
b=c
c=a+b 25
XI
Question 7
Write a program to print fibonacci series for n number of terms
The else part is executed if the condition in the while loop evaluates to False.
s a word, number
28
XI
Nested while loop
Nested loop is a loop inside another loop.
29
XI
Nested while loop
30
XI
Programs
1. Write a program to print the following patterns :
1234 2 2 * *
123 3 3 3 * * *
12 4 4 4 4 * * * *
1 5 5 5 5 5 * * * * *
31
XI
Program to generate factorial of all natural numbers
till n.
32
XI
Programs
1. Program to print all the prime numbers till n.
33
XI
Program to print all the prime numbers till n.
a=int(input("Enter a x=123045
number: ") while x%10:
while a%5!=0 x=//10
if (a%2>0): print(x,sep="--")
print(a,sep="--) else print(a)
a+=1
else: print(a)
37
XI
Solution:
a=int(input("Enter a number: ") Missing closing bracket
x=123045
while x%10:
x=//10 Wrong assignment
operator
print(x,sep="--")
else print(a) a is not defined
39
XI
Solution
a=int(input("Enter a number: ") Missing closing bracket
40
XI
Find the output of the following code:
43
XI
Solution:
44
XI
Give output :
Find the output for :
45
XI
Solution:
Enter value of n : -6
-5
46
XI
Give output :
47
XI
Solution
48
XI
Give output of the following code:
print (i)
i -= 3
52
XI
Rewrite the code using for loop:
i = 100 for i in range(100, 0, -3) :
print (i)
i -= 3
53
XI
Rewrite the code using while loop:
for i in range(4) :
54
XI
Rewrite the code using while loop:
for i in range(4) : i=0
55
XI
Rewrite the code using while loop:
num=5
56
XI
Rewrite the code using while loop:
num=5 num=5
57
XI
© CS-DEPT DPS MATHURA ROAD
Happy Learning
Thank you!!!
58
XI