Ans. 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
Programs of while loop in Python
Q2. Write a program to print first 10 integers and their squares using while loop.
Ans.
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 + 1OUTPUT :
Enter first number : 12
Enter second number : 34
12
14
16
18
20
22
24
26
28
30
32
Programs of while loop in Python
Q10. Write a program to check whether a number is prime or not using while loop.
Ans.
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.
Ans.
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.
Ans.
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
Programs of while loop in Python
Q13. Write a program to reverse the number accepted from user using while loop.
Ans.
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
Ans.
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
Programs of while loop in Python
Q15. Write a program to print the Fibonacci series till n terms (Accept n from user) using while loop.
Ans.
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.
Ans.
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
Programs of while loop in Python
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.)
Ans.
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
Q18. Write a program to add first n terms of the following series using a for loop:
Ans.
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
Programs of while loop in Python
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.
Ans.
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.
Ans.
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
Programs of while loop in Python
Q21. Write a program to find the HCF of two numbers entered from the user.
Ans.
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.
Ans.
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
Programs of while loop in Python
Q23. Write a program to convert Binary to Decimal.
Ans.
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.
Ans.
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
Programs of while loop in Python
Q25. Write a python program to sum the sequence: 1 + 1/1! + 1/2! + 1/3! + …….. + 1/n!
Ans.
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
Ans.
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
Programs of while loop in Python
Q27. Write a program to accept 10 numbers from the user and display the largest & smallest number number.
Ans.
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[0])
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.
Ans.
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
Programs of while loop in Python
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)
Ans.
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 ,
Programs of while loop in Python
Q31. Write a program to print the following series till n terms.
Ans.
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
Q33. Write a program to find the sum of following series :
Ans.
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
Programs of while loop in Python
Q34. Write a program to find the sum of following series
Ans.
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
Programs of while loop in Python
Q36. Write a program to find the sum of following series:
S = 1 + 4 – 9 + 16 – 25 + 36 – … … n terms
Ans.
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
Programs of while loop in Python
Q37. Write a Program to print all the characters in the string ‘PYTHON’ using while loop.
Ans.
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
Programs of while loop in Python
Q40. Write a python program to get the following output
Ans.
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.
Q9 is wrong
Solution for Q4 is wrong.
Sol to Q4 is incorrect
Correct Sol for Q9:
num1 = int(input(“Enter first number: “))
num2 = int(input(“Enter second number: “))
if num1 < num2:
while num1 < num2:
if num1 % 2 == 0:
print(num1, end=', ')
num1 = num1 + 1
else:
while num2 < num1:
if num2 % 2 == 0:
print(num2, end=', ')
num2 = num2 + 1
It is asked to print exclusive of both numbers. Your code prints 1st number as well.
Hi Deeraj
Please check again.
Q.13
This website is given code you use the output print infinity time.
a=int(input(“enter the numbers :”))
b=0
while(a>0):
c=a%10
e=b*10+c
d=a//10
print(e,d)
break
Really helpfull website……
why
Q5 soloution should be
n = 11
while n>=1:
print(n)
n-=1