Mind Blowing While Loop program for beginner

Here You can Find the Solution of “Mind Blowing While Loop Program for beginner”. If You Practice these Questions You can Feel Confident in this Topic of Python.

1. Write a python program to print all natural numbers from 1 to n. – using while loop

n= int(input("Enter n= "))
i=1
print("all natural numbers from 1 to n are:")
while i>=1 and i<=n:
        print(i,end=" ")
        i+=1 

Output:

Enter n= 10
all natural numbers from 1 to n are:
1 2 3 4 5 6 7 8 9 10


2. Write a python program to print all natural numbers in reverse (from n to 1). - using while loop

n= int(input("Enter n= "))
i=n
print("all natural numbers in reverse (from n to 1) are:")
while i<=n and i>=1:

        print(i,end=" ")
        i-=1

Output:

Enter n= 10
all natural numbers in reverse (from n to 1) are:
10 9 8 7 6 5 4 3 2 1


3. Write a python program to print all alphabets from a to z. - using while loop

i=97
print("all alphabets from a to z are:")
while i<=122:
    print(chr(i),end=" ")
    i+=1

Output:

all alphabets from a to z are:
a b c d e f g h i j k l m n o p q r s t u v w x y z


4. Write a python program to print all even numbers between 1 to 100. - using while loop

i=2
print("all even number between 1 to 100 are:")
while i<100:
    if i % 2==0:
        print(i, end=" ")
    i+=1

Output:

all even number between 1 to 100 are:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98


5. Write a python program to print all odd number between 1 to 100.

i=2
print("all odd number between 1 to 100 are:")
while i<100:
    if i % 2!=0:
        print(i, end=" ")
    i+=1

Output:

all odd number between 1 to 100 are:
3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99


6. Write a python program to find sum of all natural numbers between 1 to n.

n= int(input("Enter n= "))
i=2
s=0
while i<n:
s=s+i
i+=1
print("sum of all natural numbers between 1 to n= ", s)

Output:

Enter n= 10
sum of all natural numbers between 1 to n= 44


7. Write a python program to find sum of all even numbers between 1 to n.

n= int(input("Enter n= "))
i=2
s=0
while i<n:
if i % 2==0:
s=s+i
i+=1
print("sum of all even numbers between 1 to n= ", s)

Output:

Enter n= 10
sum of all even numbers between 1 to n= 20


8. Write a python program to find sum of all odd numbers between 1 to n.

n= int(input("Enter n= "))
i=2
s=0
while i<n:
if i % 2!=0:
s=s+i
i+=1
print("sum of all odd numbers between 1 to n= ", s)

Output:

Enter n= 10
sum of all odd numbers between 1 to n= 24


9. Write a python program to print multiplication table of any number.

n= int(input("Enter any number= "))
m=n
i=1
print("multiplication table of", n, "are:")
while i<=10:
    print(m, end=" ")
    m = m + n
    i+=1

Output:

Enter any number= 10
multiplication table of 10 are: 10 20 30 40 50 60 70 80 90 100


10. Write a python program to count number of digits in a number.

n= int(input("Enter any number= "))
i=0
while n!=0:
    n=n//10
    i+=1
print("Total number of digits in your number is ", i)

Output:

Enter any number= 10
Total number of digits in your number is 2


11. Write a python program to find first and last digit of a number.

n= int(input("Enter any number= "))
i=0
m=n
while n!=0:
    n=n//10
    i+=1
f=m//10**(i-1)
print("first digit of your number are ", f)
t=m//10
l=m-t*10
print("last digit of your number are ", l)

Output:

Enter any number= 123456
first digit of your number are 1 last digit of your number are 6


12. Write a python program to find sum of first and last digit of a number.

n= int(input("Enter any number= "))
i=0
m=n
while n!=0:
    n=n//10
    i+=1
f=m//10**(i-1)
t=m//10
l=m-t*10
s=f+l
print("sum of first and last digit of your number are ",s)

Output:

Enter any number= 123456

sum of first and last digit of your number are 7


13. Write a python program to calculate sum of digits of a number.

n = int(input("enter any number "))

f = n
i = 0
while n > 0:
n = n // 10
i += 1

add = 0
while i > 0:
f1 = f // (10 ** (i - 1))
add = add + f1
f2 = f - (f1 * (10 ** (i - 1)))
f = f2
i -= 1

Output:

enter any number 123456

Sum of digit of your number is 21


14. Write a python program to calculate product of digits of a number.

n = int(input("enter any number "))

f = n
i = 0
while n > 0:
    n = n // 10
    i += 1

c = i
mul = 1
while c > 0:
    f1 = f // (10 ** (c - 1))
    mul = mul * f1
    f2 = f - (f1 * (10 ** (c - 1)))
    f = f2
    c -= 1
print("Product of digit of your number is  ", mul)

Output:

enter any number 12345

Product of digit of your number is 120


Are You Understand above Topic “Mind Blowing While Loop program for beginner”?
For any query or suggestions feel free to fill comment section and send it.


More Topic of Python:
Python Program of Basic Question’s Part-1
Python Program of Basic Question’s Part-2
Top Python Program of String for beginner


You may also like...

Leave a Reply

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