# 40 Important Questions of While loop in Python (Solved) Class 11

Share with others

## d. First 10 Whole numbers

```Show AnswerAns. a)

num = 2
while(num<=20):
print(num)
num = num + 2

b)

num = 1
while(num<=20):
print(num)
num = num + 2

c)

num = 1
while(num<=10):
print(num)
num = num + 1

d)

num = 0
while(num<10):
print(num)
num = num + 1```

## 3 9 and so on

```Show AnswerAns.

num = 1
print("Numbers\t\tSquares")
while(num<=10):
print(num,"\t\t\t\t", num ** 2)
num = num + 1

OUTPUT :

Numbers     Squares
1                1
2                4
3                9
4                16
5                25
6                36
7                49
8                64
9                81
10             100
```

## 10, 20, 30 … … 300

```Show AnswerAns.

num = 10
while (num <= 300) :
print(num, end = " , ")
num = num +10```

## 105, 98, 91 ………7.

```Show AnswerAns.

num = 105
while(num <= 7) :
print(num)
num = num - 7```

## Q5. Write a program to print first 10 natural number in reverse order using while loop.

```Show AnswerAns.

num = 10
while num >= 1:
print(num)
num= num - 1```

## Q6. Write a program to print sum of first 10 Natural numbers.

```Show AnswerAns.

num = 10
sum = 0
while num >= 1:
sum = sum + num
num= num - 1
print(sum)
```

## Q7. Write a program to print sum of first 10 Even numbers.

```Show AnswerAns.

num = 2
sum = 0
while num <= 20:
sum = sum + num
num= num + 2
print(sum)```

## Q8. Write a program to print table of a number entered from the user.

```Show AnswerAns.

i = 1
num = int(input("Enter any number  : "))
while i <= 10:
print(num," * ",i," = ", num * i)
i = i+1

OUTPUT :

Enter any number  : 5
5  *  1  =  5
5  *  2  =  10
5  *  3  =  15
5  *  4  =  20
5  *  5  =  25
5  *  6  =  30
5  *  7  =  35
5  *  8  =  40
5  *  9  =  45
5  *  10  =  50```

## Q9. Write a program to find the sum of all even numbers that falls between two numbers (exclusive both numbers) entered from the user using while loop.

```Show AnswerAns.

num1 = int(input("Enter first number  : "))
num2 = int(input("Enter second number  : "))
if num1 > num2:
while(num1>num2):
if num2 % 2 == 0:
print(num2)
num2 = num2 + 1
else:
while(num1<num2):
if num1 % 2 == 0:
print(num1)
num1 = num1 + 1

OUTPUT :

Enter first number  : 12
Enter second number  : 34
12
14
16
18
20
22
24
26
28
30
32```

## Q10. Write a program to check whether a number is prime or not using while loop.

```Show AnswerAns.

num1 = int(input("Enter any number : "))
k=0
if num1 == 0 or num1 == 1:
print("Not a prime number ")
else:
i = 2
while(i<num1):
if num1 % i == 0:
k = k+1
i = i+1
if k == 0 :
print( num1,"is prime number")
else:
print(num1, "is not prime number")

OUTPUT :

Enter any number : 67
67 is prime number```

## Q11. Write a program to find the sum of the digits of a number accepted from the user.

```Show AnswerAns.

num1 = int(input("Enter any number : "))
r=0
s=0
while(num1!=0):
r = num1 % 10
s = s + r
num1 = num1//10
print("Sum of the digits are : " , s)

OUTPUT :

Enter any number : 4567
Sum of the digits are :  22```

## Q12. Write a program to find the product of the digits of a number accepted from the user.

```Show AnswerAns.

num1 = int(input("Enter any number : "))
r=0
p=1
while(num1!=0):
r = num1 % 10
p = p * r
num1 = num1//10
print("Product of the digits are : " , p)

OUTPUT :

Enter any number : 342
Product of the digits are :  24```

## Q13. Write a program to reverse the number accepted from user using while loop.

```Show AnswerAns.

num1 = int(input("Enter any number : "))
r=0
rnum=0
while(num1!=0):
r = num1 % 10
rnum = rnum * 10 + r
num1 = num1//10
print("Reverse number is : ", rnum)

OUTPUT :

Enter any number : 67023
Reverse number is :  32076```

## Q14. Write a program to display the number names of the digits of a number entered by user, for example if the number is 231 then output should be Two Three One

```Show AnswerAns.

num1 = input("Enter any number : ")
L1 = list(num1)
L = len(L1)
i=0
n = {0 : "Zero", 1 : "One", 2 : "Two", 3 : "Three", 4 : "Four", 5 : "Five", 6 : "Six", 7 : "Seven", 8 : "Eight", 9 : "Nine"}
while (i < L):
print(n[int(L1[i])], end = " ")
i = i + 1

OUTPUT :

Enter any number : 937
Nine Three Seven```

## Q15. Write a program to print the Fibonacci series till n terms (Accept n from user) using while loop.

```Show AnswerAns.

n = int(input("How many terms you want in Fibonacci Series  : "))
if n==1:
print("1")
elif n == 2:
print("1, 1")
elif n <= 0:
print("Please enter positive number greater than 0")
else:
ft = 1
st = 1
print(ft,end=" ")
print(st,end=" ")
i = 2
while(i<n):
nt = ft + st
print(nt,end = " ")
ft = st
st = nt
i = i + 1

OUTPUT :

How many terms you want in Fibonacci Series  : 7
1 1 2 3 5 8 13```

## Q16. Write a program to print the factorial of a number accepted from user.

```Show AnswerAns.

num = int(input("Enter any number : "))
f = 1
i = 1
while(i <= num):
f = f * i
i = i + 1
print("Factorial of a number is  : ", f)

OUTPUT :
Enter any number : 4
Factorial of a number is  :  24```

## Q17. Write a program to check whether a number is Armstrong or not. (Armstrong number is a number that is equal to the sum of cubes of its digits for example : 153 = 1^3 + 5^3 + 3^3.)

```Show AnswerAns.

num1 = input("Enter any number : ")
p=0
L = len(num1)
L1 = list(num1)
i = 0
while(i < L):
p = p + int(L1[i])**3
i = i + 1
if int(num1) == p:
print("Number is Armstrong")
else:
print("Number is not Armstrong")

OUTPUT :
Enter any number : 153
Number is Armstrong```

## 1/1! + 1/2! + 1/3! + …….. + 1/n!

```Show AnswerAns.

num = int(input("Enter any number  : "))
sum = 0
fact = 1
i = 1
while(i < num):
fact= fact*i
sum= sum + i/fact
i = i + 1
print("Sum is       :    ",round(sum, 2))

OUTPUT :

Enter any number  : 3
Sum is       :     2.0```

## Q19. Write a program to enter the numbers till the user wants and at the end it should display the sum of all the numbers entered.

```Show AnswerAns.

ch = 'Y'
sum = 0
while ch.upper() == 'Y':
num = int(input("Enter any number  : "))
sum = sum + num
ch=input("Do you wish to continue(Y,N)?: ")
print("Sum of all the numbers is  : ", sum)

OUTPUT :

Enter any number  : 1
Do you wish to continue(Y,N)?: y
Enter any number  : 2
Do you wish to continue(Y,N)?: y
Enter any number  : 3
Do you wish to continue(Y,N)?: n
Sum of all the numbers is  :  6```

## Q20. Write a program to enter the numbers till the user enter ZERO and at the end it should display the count of positive and negative numbers entered.

```Show AnswerAns.

ch = 'Y'
sump = 0
sumn = 0
num=1
while (num!=0):
num = int(input("Enter any number  : "))
if num > 0 :
sump = sump + num
else:
sumn = sumn + num
print("Sum of all the positive numbers is  : ", sump)
print("Sum of all the negative numbers is  : ", sumn)

OUTPUT :

Enter any number  : 5
Enter any number  : -5
Enter any number  : 3
Enter any number  : -3
Enter any number  : 4
Enter any number  : -4
Enter any number  : 0
Sum of all the positive numbers is  :  12
Sum of all the negative numbers is  :  -12```

## Q21. Write a program to find the HCF of two numbers entered from the user.

```Show AnswerAns.

num1 = int(input("Enter first number : "))
num2 = int(input("Enter second number : "))
rem = 1
if num1 > num2 :
while rem!=0:
rem = num1 % num2
if rem == 0:
hcf = num2
else:
num1 = num2
num2 = rem
else :
while rem!=0:
rem = num2 % num1
if rem == 0:
hcf = num1
else:
num2 = num1
num1 = rem
print("HCF of two numbers is  : ", hcf)

OUTPUT :

Enter first number: 2
Enter second number: 8
HCF is 2```

## Q22. Write a program to convert Decimal to Binary.

```Show AnswerAns.

```num = int(input("Enter a positive number: "))
bin = 0
p = 1
n = num
while n>0:
rem = int(n % 2)
bin = bin + rem * p
p = p*10
n = n/2
print("Binary number of",num,"is",bin)```

OUTPUT :

Enter a positive number: 15
Binary number of 15 is 1111```

## Q23. Write a program to convert Binary to Decimal.

```Show AnswerAns.

bin = input("Enter a binary number: ")
dec=0
p=0
for i in reversed(bin):
dec = dec + int(i)*pow(2,p)
p=p+1
print("Decimal number of binary",bin," number is : ",dec)

OUTPUT :

Enter a binary number: 1111
Decimal number of binary 1111 number is : 15```

## Q24. Write a program to check whether a number is palindrome or not.

```Show AnswerAns.

num1 = int(input("Enter any number : "))
onum = num1
r=0
rnum=0
len = len(str(num1))
i = 0
while(i<len):
r = num1 % 10
rnum = rnum * 10 + r
num1 = num1//10
i = i + 1
if onum == rnum:
print("Number is Palindrome")
else:
print("Number is not Palindrome")

OUTPUT :

Enter any number : 1234321
Number is Palindrome```

## Q25. Write a python program  to sum the sequence:1 + 1/1! + 1/2! + 1/3! + …….. + 1/n!

```Show AnswerAns.

n = int(input("enter a nth term = "))
f = 1
s = 1
i=1
while(i<=n):
f = f * i
s = s + 1 / f
i=i+1
print("sum of sequence = ", s)

OUTPUT :

enter a nth term = 5
sum of sequence =  2.7166666666666663```

## Q26. Write a program to accept 10 numbers from the user and display it’s average

```Show AnswerAns.

s=0
i=0
while(i<10):
num = int(input("Enter number : "))
s = s + num
i = i + 1
print("Average is : ", s/10)

OUTPUT :

Enter number : 1
Enter number : 2
Enter number : 3
Enter number : 4
Enter number : 5
Enter number : 6
Enter number : 7
Enter number : 8
Enter number : 9
Enter number : 10
Average is :  5.5```

## Q27. Write a program to accept 10 numbers from the user and display the largest & smallest number number.

```Show AnswerAns.

L = [ ]
i = 0
while(i<10):
num = int(input("Enter number : "))
L.append(num)
i = i + 1
L.sort()
print("Largest number is : ", L[-1])
print("Smallest number is : ", L)

Enter number : 1
Enter number : 2
Enter number : 3
Enter number : 4
Enter number : 5
Enter number : 6
Enter number : 7
Enter number : 8
Enter number : 9
Enter number : 99
Largest number is :  99
Smallest number is :  1```

## Q28. Write a program to display sum of odd numbers and even numbers separately that fall between two numbers accepted from the user.(including both numbers) using while loop.

```Show AnswerAns.

num1 = int(input("Enter first number  : "))
num2 = int(input("Enter second number  : "))
se = 0
so = 0
if num1 > num2 :
while(num2 <= num1):
if num2%2 == 0:
se = se + num2
num2 = num2 + 1
else:
so = so + num2
num2 = num2 + 1
else :
while(num1 <= num2):
if num1 % 2 == 0:
se = se + num1
num1 = num1 + 1
else:
so = so + num1
num1 = num1 + 1
print("Sum of even numbers is : ", se)
print("Sum of odd numbers is : ", so)

OUTPUT:

Enter first number  : 16
Enter second number  : 12
Sum of even numbers is :  42
Sum of odd numbers is :  28```

## Q29. Write a program to display all the numbers which are divisible by 13 but not by 3 between 100 and 500.(exclusive both numbers)

```Show AnswerAns.

i = 101
while(i < 500):
if i % 13 == 0 and i % 3 != 0:
print(i)
i = i + 1

OUTPUT :
104
130
143
169
182
208
221
247
260
286
299
325
338
364
377
403
416
442
455
481
494

```

## 2 , 22 , 222 , 2222 _ _ _ _ _ n terms

```Show AnswerAns.

n = int(input("Enter value of n  : "))
st = '2'
i = 0
while(i<n):
print(st, end=" , ")
st =st + '2'
i = i + 1

OUTPUT :

Enter value of n  : 5
2 , 22 , 222 , 2222 , 22222 ,```

## 1 4 9 16 25 _ _ _ _ _ n terms.

```Show AnswerAns.

n = int(input("Enter value of n : "))
i = 1
while(i <= n):
print(i**2, end = " , ")
i = i + 1

OUTPUT :

Enter value of n : 5
1 , 4 , 9 , 16 , 25 ,```

## 1 + x/1! + x2/2! + ……….xn/n!

```Show AnswerAns.

def fac(n):
f = 1
for i in range(1,n+1):
f = f * i
return(f)
sum = 1
n = int(input("Enter value of n : "))
x = int(input("Enter value of x : "))
i = 1
while(i < n):
sum = sum + x**i/fac(i)
i = i + 1
print(sum)

OUTPUT :

Enter value of n : 5
Enter value of x : 3
16.375 ```

## x + x2/2 + ……….xn/n

```Show AnswerAns.

n = int(input("enter a nth term = "))
x = int(input("Enter value of x : "))
sum = 0
i = 1
while(i<=n):
sum = sum + 2**i/i
i = i + 1
print(round(sum,2))

OUTPUT :

enter a nth term = 3
Enter value of x : 2
6.67```

## 1 + 8 + 27 …………n terms

```Show AnswerAns.

n = int(input("Enter number of terms : "))
s = 0
i = 1
while(i <= n):
s = s + i ** 3
i = i + 1
print(s)

OUTPUT:

Enter number of terms  : 4
100```

## 1 + 2 + 6 + 24 + 120 . . . . . n terms

```Show AnswerAns.

n = int(input("Enter number of terms  : "))
s = 0
pr = 1
i = 1
while(i<=n) :
pr = i * pr
print(pr, end = " + ")
s = s + pr
i = i + 1
print(" = ", s)

OUTPUT :

Enter number of terms  : 5
1 + 2 + 6 + 24 + 120 +  =  153
```

## S = 1 + 4 – 9 + 16 – 25 + 36 – … … n terms

```Show AnswerAns.

n = int(input("Enter number of terms : "))
s = 0
sp = 1
sn = 0
i = 2
while(i <= n):
if i % 2 == 0:
sp = sp + i ** 2
i = i + 1
else :
sn = sn + i ** 2
i = i + 1
print(sp - sn)

OUTPUT :

Enter number of terms : 6
23 ```

## Q37. Write a Program to print all the characters in the string ‘PYTHON’ using while loop.

```Show AnswerAns.

str = "PYTHON"
i = 0
while(i<len(str)):
print(str[i])
i = i + 1

OUTPUT :

P
Y
T
H
O
N

```

## Q38. Write a program to print only odd numbers from the given list using while loop. L = [23, 45, 32, 25, 46, 33, 71, 90]

```Show AnswerAns.

L = [23, 45, 32, 25, 46, 33, 71, 90]
i = 0
while(i < len(L)) :
if L[i] % 2 != 0 :
print(L[i])
i = i + 1

OUTPUT :

23
45
25
33
71```

## Q39. Write a program to print all the factors of a number using for loop.

```Show AnswerAns.

n = int(input("Enter any number"))
print("Factors are : ")
i = 1
while(i <= n):
if n % i == 0 :
print(i)
i = i + 1

OUTPUT :

Enter any number8
Factors are :
1
2
4
8```

## Q40. Write a python program to get the following output

1—–49

2—–48

3—–47

. .

. .

. .

48—–2

49—–1

```Show AnswerAns.

i=1
j=49
while(i<=49 and j>=1):
print(i,"---",j)
i=i+1
j = j - 1```

## Programs of while loop in Python

Disclaimer : I tried to give you the correct Solution of “programs of while Loop in Python” , but if you feel that there is/are mistakes in the solution of “programs of while Loop in Python given above, you can directly contact me at csiplearninghub@gmail.com.

## 40 Practice Questions on Data Structure

Class 12 Computer Science Sample Paper 2020-2021.

Class 12 Computer Science Sample Paper Marking Scheme

Class 12 Computer Science Test Series

Share with others