0% found this document useful (0 votes)
11 views58 pages

Class 11 - Presentation # 5 - Iterative Statements - While Loop

The document provides an overview of iterative statements in programming, specifically focusing on the 'while' loop in Python. It explains the structure of a while loop, including initialization, test expression, body, and update expression, along with examples of how to implement loops for various tasks. Additionally, it covers nested loops and includes several programming exercises for practice.

Uploaded by

Mr.Saksham Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views58 pages

Class 11 - Presentation # 5 - Iterative Statements - While Loop

The document provides an overview of iterative statements in programming, specifically focusing on the 'while' loop in Python. It explains the structure of a while loop, including initialization, test expression, body, and update expression, along with examples of how to implement loops for various tasks. Additionally, it covers nested loops and includes several programming exercises for practice.

Uploaded by

Mr.Saksham Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

Computational Thinking

© CS-DEPT DPS MATHURA ROAD


and Programming - 1
Iterative Statements
(While Loop)

XI
Introduction
Iteration means to do something repeatedly. Iteration is also called loop.

© CS-DEPT DPS MATHURA ROAD


In programming there are many situations where we have to repeat a set of
statements.

❖ Suppose we have to input 20 numbers. Should we write input() statement 20


times, or we should have some way so that we write the input() statement once
and it is automatically executed 20 times?

This repetition may be for a specified number of times, or as long as a specified


condition is True. All programming language provides some iterative or looping
constructs which are used to automatically repeat a set of statements.

2
XI
Types of Loops

© CS-DEPT DPS MATHURA ROAD


There are 2 looping constructs available in Python.

Loops

for while

3
XI
while loop
while is a loop used for repeated execution as long as a specified expression

© CS-DEPT DPS MATHURA ROAD


(condition) is true. while test-expression:

Syntax : Statements 1
[else:
Statements 2]

● test expression is a Boolean expression which evaluates to True or False. Such


an expression represents a condition.
● else is an optional clause of while loop Execution.

4
XI
while loop
While loop with else clause While loop without else clause

© CS-DEPT DPS MATHURA ROAD


true true
expression Statements 1 Statements 1
expression

false false

Statements 2 Out of loop

Out of loop

5
XI
Loop Control Elements
Every while loop has its elements that control and govern its execution. A while

© CS-DEPT DPS MATHURA ROAD


loop has four elements that have different purposes:

● Initialisation expression : Before entering a loop, the loop variable (also


called control variable) must be initialised in an initialisation expression.For
a while loop, the initialisation expression is mentioned outside the loop
before it starts.
● Test expression : It is the condition whose value decides whether the loop
body will be executed or not. If the test expression evaluates to True, the
loop body gets executed, otherwise the loop is terminated.

6
XI
Loop Control Elements

© CS-DEPT DPS MATHURA ROAD


● Body of the loop :The statements that have to be executed repeatedly
form the body of the loop. The body of the loop may be a simple statement
or a compound statement.
● Update expression : The statement that changes the value of the loop
variable is called an update expression. This expression is mentioned inside
the body of the loop.

7
XI
while loop

© CS-DEPT DPS MATHURA ROAD


Initialisation expression

while test expression :

Statements

….. Body of the loop

…..

…..

Update expression

8
XI
Some important points:
In a while loop, the loop control variable should be initialized before the

© CS-DEPT DPS MATHURA ROAD


loop begins as an uninitialised variable cannot be used in the test
condition (expression)

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

© CS-DEPT DPS MATHURA ROAD


expression expression

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

© CS-DEPT DPS MATHURA ROAD


num=1 First 10 Natural Numbers
print("First 10 Natural Numbers") 1
while num<=10: 2
print(num) 3
num=num+1 .
.
.
10

11
XI
Question 1
Write a program to print all even numbers from 1 to 10 and find their sum too.

© CS-DEPT DPS MATHURA ROAD


num=2 Even numbers are:
s=0 2
print("Even Numbers are :") 4
while num<=10: 6
print(num) 8
s=s+num 10
num=num+2 Sum of even numbers
print("Sum of even numbers :",s) :40

12
XI
Question 2
Write a program to print table of number n.

© CS-DEPT DPS MATHURA ROAD


num=int(input("Enter the number:")) Enter the number: 6
print("Table of number :") Table of number :
i=1 6x1=6
while i<=10: 6x2=12
print(num,"x",i,"=",num*i) 6x3=18
i=i+1 .
.
.
6x10=60

13
XI
Question 3
Write a program input a number and then find the factorial of a number.

© CS-DEPT DPS MATHURA ROAD


Factorial of number n is calculated as : n!=n×(n−1)×(n−2)...×2×1

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


i=1 Factorial of number :
f=1 120
while i<=num:
f=f*i
i=i+1
print("Factorial of number :",f)

14
XI
Programs
1. Print all the natural numbers from m to n.

© CS-DEPT DPS MATHURA ROAD


2. Input a number n and then print all natural numbers from n to 1.(in reverse order)
3. Input a number n and then print all the odd numbers from 1 to n along with their
sum.
4. Input a number n and then print all its factors.
5. Input a number and then print the reverse of the number.
6. Input a number and check if it a palindrome number or not
7. Input a number and then print if the number is an armstrong number or not .
Example : ( 153 = 13+53+33 )
8. Input a number and then print if the number is a perfect number or not.
Example : ( 6 =1+2+3 )
9. Input the number and then print the Fibonacci series till n terms
10. Input a number and then check if it is a prime number or not.
15
XI
Question 1
Write a program to print all the natural numbers from m to n.

© CS-DEPT DPS MATHURA ROAD


m=int(input("Enter the number m:")) Enter the number m: 10
n=int(input("Enter the number n:")) Enter the number n: 16
print("Natural numbers are:") Natural numbers are:
while m<=n: 10
11
print(m)
12
m=m+1
13
14
15
16

16
XI
Question 2
Write a program to print all the natural numbers from m to 1.(Reverse Order)

© CS-DEPT DPS MATHURA ROAD


m=int(input("Enter the number m:")) Enter the number m: 7
print("Natural numbers are:") Natural numbers are:
while m>=1: 7
print(m) 6
m=m-1 5
4
3
2
1

17
XI
Question 3
Write a program to input a number n and then print all the odd numbers from 1 to m

© CS-DEPT DPS MATHURA ROAD


along with their sum.
m=int(input("Enter the number m:")) Enter the number m: 5
print("Natural numbers are:") Natural numbers are:
sum=0 1
i=1 3
5
while i<=m:
Sum : 9
print(m)
sum=sum+m
m=m+2
print("Sum :",sum)

18
XI
Question 4
Write a program to input a number n and then print all its factors.

© CS-DEPT DPS MATHURA ROAD


n=int(input("Enter the number n:")) Enter the number m: 15
i=1 Factors are:
print("Factors are:") 1
while i<=n: 3
if n%i==0: 5
print(i) 15
i=i+1

19
XI
Question 5
Write a program to input a number n and then print the sum of all its factors.

© CS-DEPT DPS MATHURA ROAD


m=int(input("Enter the number m:")) Enter the number m: 15
i=1 Factors are:
1
sum=0
3
print("Factors are:")
5
while i<=m:
15
if m%i==0: The sum of the factors is
print(i) 28
sum+=i
i=i+1
print("The sum of the factors is :", sum)
20
XI
Question 5
Write a program to input a number and find the reverse of the number.

© CS-DEPT DPS MATHURA ROAD


m=int(input("Enter the number:")) Enter the number: 2309
rev=0 Reverse Number : 9032
while m>0:
dig=m%10
rev=rev*10+dig
m=m//10
print("Reverse of number :",rev)

21
XI
Explanation :

© CS-DEPT DPS MATHURA ROAD


Number n : 2309, rev=0

Iteration dig=n%10 rev=rev*10+dig n

I 9 9 230

II 0 90 23

III 3 903 2

IV 2 9032 0

Test condition: while n > 0:


Reverse Number 22
XI
Question 5
Write a program to input a number and check if it a palindrome number or not.

© CS-DEPT DPS MATHURA ROAD


A palindrome is a word or number which reads the same backward as forward.
For example: 919 is a palindrome
s a word, number

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 =

© CS-DEPT DPS MATHURA ROAD


Armstrong
13+53+33 )

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

© CS-DEPT DPS MATHURA ROAD


The Fibonacci Sequence is the series of numbers:
0, 1, 1, 2, 3, 5, 8, 13

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

© CS-DEPT DPS MATHURA ROAD


nterms = int(input("Enter the number of terms : ")) Enter a number: 6
n1, n2 = 0, 1 0
count = 0
1
if nterms <= 0:
print("Please enter a positive integer") 1
s a word, number 2
elif nterms == 1:
print("Fibonacci series upto",nterms," :") 3
print(n1) 5
else:
print("Fibonacci series :")
print(n1)
print(n2)
while count < nterms-2:
n3 = n1 + n2
print(n3)
n1 = n2
n2 = n3
count =count+1
26
XI
Question 8
Write a program to check whether number is prime or not.

© CS-DEPT DPS MATHURA ROAD


Prime number is one which is only divisible by 1 or itself

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


flag=0 Prime Number
s a word, number
i=2
while i<num:
if num % i==0:
flag=1
break
i=i+1
if flag==0:
print("Prime Number")
else:
print("Not a Prime Number")
27
XI
while loop with else clause

The else part is executed if the condition in the while loop evaluates to False.

© CS-DEPT DPS MATHURA ROAD


Example :
counter=0 Output :
while counter<3:
print(“Inside loop”)
Inside loop
counter=counter+1 Inside loop
else: Inside loop
print(“Inside else”) Inside else

s a word, number
28
XI
Nested while loop
Nested loop is a loop inside another loop.

© CS-DEPT DPS MATHURA ROAD


Example :

n=int(input('Enter the no of lines:')) Output :


i=1
Enter the no of lines:5
while i<=n: 1
j=1
while j<=i: 12
print(j,end='') 123
j=j+1
1234
print( )
i=i+1 12345

29
XI
Nested while loop

© CS-DEPT DPS MATHURA ROAD


n=int(input('Enter the no of lines:')) Value of i Value of j
i=1
1 1
while i<=n:
j=1 2 12
while j<=i:
3 123
print(j,end='')
j=j+1 4 1234
print( )
5 12345
i=i+1

30
XI
Programs
1. Write a program to print the following patterns :

© CS-DEPT DPS MATHURA ROAD


a) 12345 b) 1 c) *

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.

© CS-DEPT DPS MATHURA ROAD


n=int(input("Enter the value of n :")) Enter the value of n :5
i=1 Factorial of number 1 is 1
Factorial of number 2 is 2
while i<=n:
Factorial of number 3 is 6
j=i Factorial of number 4 is 24
fact=1 Factorial of number 5 is 120
while j>=1:
fact=fact*j
j-=1
print("Factorial of number ",i," is ",fact)
i=i+1

32
XI
Programs
1. Program to print all the prime numbers till n.

© CS-DEPT DPS MATHURA ROAD


2. Program to print all armstrong numbers which lie between m and n.
3. Program to print all palindrome numbers till n.
4. Program to print the factorial of all numbers from 1 till n where n is input by
the user

33
XI
Program to print all the prime numbers till n.

© CS-DEPT DPS MATHURA ROAD


n=int(input("Enter the value of n :")) Enter the value of n :5
print("Prime Numbers are :") Prime numbers are:
i=1 2
while i<=n: 3
flag=0 5
j=2
while j<i:
if i % j==0:
flag=1
break
j=j+1
if flag==0:
print(i)
i=i+1
34
XI
Print Armstrong numbers between m and n.

© CS-DEPT DPS MATHURA ROAD


m=int(input("Enter the value of m :")) Enter the value of m :5
n=int(input("Enter the value of n :")) Enter value of n: 200
print("Armstrong Numbers are :") Armstrong numbers are:
i=m
153
while i<=n:
sum = 0
t = i
while t > 0:
digit = t % 10
sum += digit ** 3
t //= 10
if i == sum:
print(i)
i=i+1
35
XI
Print all palindrome numbers till n.

© CS-DEPT DPS MATHURA ROAD


m=int(input("Enter the value of m :")) Enter the value of m :20
print("Palindrome Numbers are :") Palindrome Numbers are:
i=1
1
while i<=m:
2
rev = 0
t = i 3
while t > 0: 4
digit = t % 10 5
rev=rev*10+digit 6
t //= 10 7
if i == rev: 8
print(i) 9
i=i+1 11
36
XI
Find the errors in the following code :
Code 1:

© CS-DEPT DPS MATHURA ROAD


Code 2:

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

© CS-DEPT DPS MATHURA ROAD


while a%5!=0 Missing colon
if (a%2>0):
print(a,sep="--) Missing double quotes
a+=1
else: print(a)

x=123045
while x%10:
x=//10 Wrong assignment
operator
print(x,sep="--")
else print(a) a is not defined

Wrong indent and Missing colon 38


XI
Find the errors in the following code :

© CS-DEPT DPS MATHURA ROAD


a=int(input("Enter a number: ")
b=int(input("Enter another number: ")
if a>b: step=-1
else: step=1
while a!=b:
if (a+b%2>0):
print(a+b,sep="--’)
a=+step

39
XI
Solution
a=int(input("Enter a number: ") Missing closing bracket

© CS-DEPT DPS MATHURA ROAD


b=int(input("Enter another number: ")
if a>b: step=-1
else: step=1
while a!=b:
if (a+b%2>0):
print(a+b,sep="--’)
a=+step Wrong quotation mark(‘)

Incorrect assignment operator(=+)

40
XI
Find the output of the following code:

© CS-DEPT DPS MATHURA ROAD


a=int(input('Enter a Find the output for :
number:')) 1. a,b=4,10
b=int(input('Enter a 2. a,b=2,-4
number:'))
start,end=a,b
if a>b:
start,end=b,a
i=start
while i<=end:
if (i%start==0):
print(i,end='-')
i+=1
print(i)
41
XI
Solution:

© CS-DEPT DPS MATHURA ROAD


a=int(input('Enter a Find the output for :
number:')) 1. a,b=4,10
b=int(input('Enter a 2. a,b=2,-4
number:'))
start,end=a,b Enter a number : 4
if a>b: Enter a number : 10
start,end=b,a 4-8-11
i=start
while i<=end:
if (i%start==0): Enter a number : 2
print(i,end='-') Enter a number : -4
i+=1 -4-0-3
print(i)
42
XI
Give output of the following code:

© CS-DEPT DPS MATHURA ROAD


a=10
b=a-6
if a>b:
step=-1
else: step=1
while a!=b:
if (a+b%2>0):
print(a+b,sep='--')
a+=step

43
XI
Solution:

© CS-DEPT DPS MATHURA ROAD


a=10 14
b=a-6 13
if a>b: 12
step=-1
11
else: step=1
while a!=b: 10
if (a+b%2>0): 9
print(a+b,sep='--')
a+=step

44
XI
Give output :
Find the output for :

© CS-DEPT DPS MATHURA ROAD


a=int(input("Enter the value of n : "))
while a%5!=0:
10
if a%2>0:
-6
print(a,sep="-")
a+=1
else:print(a)

45
XI
Solution:

© CS-DEPT DPS MATHURA ROAD


a=int(input("Enter the value of n : ")) Find the output for :
while a%5!=0:
10
if a%2>0:
print(a,sep="-")
-6
a+=1
else:print(a) Enter value of n : 10
10

Enter value of n : -6
-5

46
XI
Give output :

© CS-DEPT DPS MATHURA ROAD


x=123045
while x%10:
x//=10
print(x,sep="--")
else:print(x)

47
XI
Solution

© CS-DEPT DPS MATHURA ROAD


x=123045 12304
while x%10: 1230
x//=10 1230
print(x,sep="--")
else:print(x)

48
XI
Give output of the following code:

© CS-DEPT DPS MATHURA ROAD


choice="y" INPUT
while choice=="y": A = 10
A = int(input("A : ")) B = 20
B = int(input("B: "))
if A >= B:
print("Stop!")
OUTPUT:
break
C = A
while C <= B: 10*11*12*13*14*15*16*17*18*19*20*
print(C,end = "*")
C+=1
print()
choice=input("Add more")
49
XI
Rewrite the code using for loop:
num=10

© CS-DEPT DPS MATHURA ROAD


sum=0
count=0
while num > 0 :
count += 1
sum += num
num –= 2
if count == 10 :
print (sum/float(count))
break
50
XI
Rewrite the code using for loop:
num=10 num=10

© CS-DEPT DPS MATHURA ROAD


sum=0 sum=0
count=0 count=0
while num > 0 :
for i in range(num,0,-2):
count += 1
count+=1
sum += num
sum+=i
num –= 2
if count==10:
if count == 10 :
print(sum/float(count))
print (sum/float(count))
break
break
51
XI
Rewrite the code using for loop:
i = 100

© CS-DEPT DPS MATHURA ROAD


while (i > 0) :

print (i)

i -= 3

52
XI
Rewrite the code using for loop:
i = 100 for i in range(100, 0, -3) :

© CS-DEPT DPS MATHURA ROAD


while (i > 0) : print (i)

print (i)

i -= 3

53
XI
Rewrite the code using while loop:
for i in range(4) :

© CS-DEPT DPS MATHURA ROAD


for j in range(5):
if i + 1 == j or j + 1 == 4 :
print ("+", end = ' ')
else :
print ("o", end = ' ')
print()

54
XI
Rewrite the code using while loop:
for i in range(4) : i=0

© CS-DEPT DPS MATHURA ROAD


for j in range(5): while i < 4:
j=0
if i + 1 == j or j + 1 == 4 :
while j < 5:
print ("+", end = ' ')
if i + 1 == j or j + 1 == 4 :
else :
print ("+", end = ' ')
print ("o", end = ' ') else :
print() print ("o", end = ' ')
j += 1
i += 1
print()

55
XI
Rewrite the code using while loop:
num=5

© CS-DEPT DPS MATHURA ROAD


min = 0
max = num
if num < 0 :
min = num
max = 0
for i in range(min, max + 1):
sum += i

56
XI
Rewrite the code using while loop:
num=5 num=5

© CS-DEPT DPS MATHURA ROAD


min = 0 min = 0
max = num
max = num
if num < 0 :
if num < 0 :
min = num
min = num
max = 0
max = 0 i = min
for i in range(min, max + 1): while i <= max:
sum += i sum += i
i += 1

57
XI
© CS-DEPT DPS MATHURA ROAD
Happy Learning
Thank you!!!

58
XI

You might also like