Real Time Example Program of While Loop

Definition of While Loop:

Repeats a block of code as long as a specified condition is true.


Real Time Examples program for While Loop :


Question 1: Using while loop to print the count from 1 to 5 

Program:
counter = 1

while counter <= 5:
    print("Count :" + str(counter))
    counter += 1
    
Output:
Count :1
Count :2
Count :3
Count :4
Count :5
    


Question 2 : Print a number from 1 to 5 using while loop.

Program:
number = 1
while number <= 5:
    print(number)
    number += 1
    
Output:
1
2
3
4
5
    

Question 3: write a loop statement to print the following series 10,20,30,40,50,60,....200

Program:
number = 10
while number <= 200:
    print(number, end=",")
    number += 10
    
Output:
10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,
    


Question 4 : Write a program to print first 10 natural numbers in reverse order.

Program :
natural_number = 10
while natural_number > 0:
    print(natural_number, end=",")
    natural_number -= 1
    
Output:
10,9,8,7,6,5,4,3,2,1,
    


Question 5 : Write a program to find the factorial of a number 

Program :
number = 5
fact = 1
while number > 0:
    fact = fact * number
    number = number - 1
print("Factorial of 5 is :", fact)
    
Output:
Factorial of 5 is : 120
    

Post a Comment

0 Comments