While Loop and Examples in Python

While-Loop-in-Python-with-Examples

Iterative statements are decision control statement that are used to repeat the execution list of statements. In python there are two types of iterative statements.

  • while loop
  • for loop

While loop -repeats the statements while condition is True.

#Q1 . WAP to print first hundred natural numbers.

#Ex . WAP to print first hundred natural numbers
x=0 # initialization of x
while x<=100:
    print(x,end=' ')
    x=x+1
print('\nEnd of While Loop')

Q2. WAP to print 20 horizontal (*).

#Ex of while loop -repeats the statments while condition is True
x=0
while x<=100:
    print(x,end=' ')
    x=x+1
print('\nEnd of While Loop')

Q3.Wap print sum and average of first 10 numbers.

#Wap print sum and average of first 10 numbers
'''
1+2+3+4+5+6+7+8+9+10=55
Get avg i.e. 55/10 =5.5
'''
i=1
sum=0
while i<=10:
    sum=sum+i
    i=i+1
print("sum=",sum)
print("Average", sum/10)

Q4. WAP to print reverse of a number .

# WAP to print  reverse of a number
# supposed entered number is  123 then print reversed number is 321
num=int(input('Please enter the number')) # input method read the data entered by keyboard
print('Reversed Number is ',)
while num!=0:
    digit=num%10
    print(digit,end='')
    num=num//10

Q5.WAP using a while loop that ask the user to enter for a number, and prints a countdown from that number to zero.

n=int(input('Enter the number'))
while n>0:
    print(n,end=' ')
    n=n-1

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top