While-loop-in-Python .

In this session i have discussed while loop in Python .


The while loop is very fast loop in Python .

While Loop Syntax :

    while(test condition):
        loop body
    
    

While loop is a entry control loop . In this loop first check the condition if condition is true then the loop body will be executed . After execution of the loop body control once again check if condition if it is true then control again come inside the loop body this process continue .. until the condition become false .


Example : Write a python program that print the number from 1-10 using while loop .

i=1
while(i<=10):
    print(i,end=" ")
    i=i+1
    
    OUTPUT : 1 2 3 4 5 6 7 8 9 10
    

Example : Write a python program that print the table of 2 using while loop .

i=1
t=0
while(i<=10):
    t=i*2;
    print(t,end=" ")
    i=i+1
    
    OUTPUT : 2 4 6 8 10 12 14 16 18 20
    

Example : Write a python program that print the even number using while loop .

i=1
while(i<=10):
    if(i%2==0):
        print(i,end=" ")
        i=i+1               
    
    OUTPUT : 2 4 6 8 10
    

Example : Write a python program that print the odd number using while loop .

i=1
while(i<=10):
    if((i%2)!=0):
        print(i,end=" ")
    i=i+1                
    
    OUTPUT : 1 3 5 7 9
    

Example : Write a python program that print the reverse number of any given number using while loop . 123=321

n = 123 # reverse the number 321
rem,rev=0;
while (n>0):
    rem = n % 10
    rev = (rev *10)+rem
    n = n // 10
print(rev)
    
    OUTPUT : 321
    

Example : Write a python program that sum the individual digits of any given number using while loop . 123=6

n = 123 
rem=0
sum=0
while (n>0):
    rem = n % 10
    sum=sum+rem
    n = n / 10
print(sum)
    
    OUTPUT : 6
    


Example : Write a python program that print the palindrome number using while loop . 121=121

n = 121
k = n
rem=0
rev=0
while (n>0):
    rem = n % 10
    rev = (rev*10)+rem
    n = n // 10
if (k == rev):
    print("Number is palindrome")
else:
    println("Number is not palindrome")
    
    OUTPUT : Number is palindrome
    


Example : Write a python program that print the armstrong number using while loop . 153=153 means 1*1*1+5*5*5+3*3*3 .

n = 153
k = n
rem=0
sum=0
while (n>0):
    rem = n % 10
    sum = sum+(rem*rem*rem)
    n = n // 10
if (k == sum):
    print("Number is armstrong")
else:
    print("Number is not armstrong")
    
    OUTPUT : Number is armstrong
    


Example : Write a program that print the fibonacci series using while loop . i.e 0 1 1 2 3 5 8 .

sum = 0
f0 = 0
f1 = 1
i = 1
print(f0)
print(f1)
while (i<6):
    sum = f0 + f1
    f0 = f1
    f1 = sum
    i=i+1
    print(sum)
    
    0 
    1
    1
    2 
    3 
    5 
    8
    

Example : Write a python program that print the table 01-10 using while loop .

i = 1
j = 1
t = 0
while (i <= 10):
    j = 1
    while(j<=10):
        t = i * j;
        if(t<10):
            print("0%d"%(t),end=" ")
        else:
            print(t,end=" ")
        j=j+1
    i=i+1
    print("\n")
    

    OUTPUT :  01 02 03 04 05 06 07 08 09 10
              02 04 06 08 10 12 14 16 18 20
              03 06 09 12 15 18 21 24 27 30
              04 08 12 16 20 24 28 32 36 40
              05 10 15 20 25 30 35 40 45 50
              06 12 18 24 30 36 42 48 54 60
              07 14 21 28 35 42 49 56 63 70
              08 16 24 32 40 48 56 64 72 80
              09 18 27 36 45 54 63 72 81 90
              10 20 30 40 45 60 70 80 90 100
    

Welcome to AndroidATPoint.

back image                     next image