Ans.
num1 = int(input("Enter first number"))
num2 = int(input("Enter second number"))
if num1 > num2 :
for i in range(num2+1 , num1):
if i%2 == 0:
print(i)
else :
for i in range(num1 + 1, num2):
if i % 2 == 0:
print(i)
OUTPUT :
Enter first number34
Enter second number12
14
16
18
20
22
24
26
28
30
32
Q10. Write a program to check whether a number is prime or not.
Ans.
num1 = int(input("Enter any number : "))
k=0
if num1 == 0 or num1 == 1:
print("Not a prime number ")
else:
for i in range(2,num1):
if num1 % i == 0:
k = k+1
if k == 0 :
print(num1, "is prime number")
else:
print(num1, "is not prime number")
OUTPUT :
Enter any number : 7
7 is prime number
For loop Programs in Python
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
len = len(str(num1))
for i in range(len):
r = num1 % 10
s = s + r
num1 = num1//10
print("Sum of the digits are : " , s)
OR
num = input("Enter any number : ")
f = 0
for i in num:
f = f + int(i)
print("Sum of the digits is : ", f)
OUTPUT :
Enter any number : 564
Sum of the digits are : 15
Q12. Write a program to find the product of the digits of a number accepted from the user.
Ans.
num = input("Enter any number : ")
f = 1
for i in num:
f = f * int(i)
print("Product of the digits is : ", f)
OUTPUT :
Enter any number : 231
Product of the digits is : 6
For loop Programs in Python
Q13. Write a program to reverse the number accepted from user.
Ans.
num1 = int(input("Enter any number : "))
r=0
rnum=0
len = len(str(num1))
for i in range(len):
r = num1 % 10
rnum = rnum * 10 + r
num1 = num1//10
print("Reverse number is : ", rnum)
OR
num1 = input("Enter any number : ")
print("Reverse number is : ", num1[: : -1])
OUTPUT :
Enter any number : 342
Reverse number is : 243
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 : ")
L = list(num1)
n = {0 : "Zero", 1 : "One", 2 : "Two", 3 : "Three", 4 : "Four", 5 : "Five", 6 : "Six", 7 : "Seven", 8 : "Eight", 9 : "Nine"}
for i in L:
print(n[int(i)], end = " ")
OUTPUT :
Enter any number : 5467
Five Four Six Seven
For loop Programs in Python
Q15. Write a program to print the Fibonacci series till n terms (Accept n from user)
Ans.
def fib(n):
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=" ")
for i in range(2,n):
nt = ft + st
print(nt,end = " ")
ft = st
st = nt
nt = int(input("How many terms you want in Fibinacci Series"))
fib(nt)
Q16. Write a program to print the factorial of a number accepted from user.
Ans.
num = int(input("Enter any number : "))
f = 1
for i in range(1,num+1):
f = f * i
print("Factorial of a number is : ", f)
OUTPUT :
Enter any number : 6
Factorial of a number is : 720
For loop Programs 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
for i in num1:
p = p + int(i)**3
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 convert binary to decimal.
Ans.
bin = input("Enter the number in binary : ")
dec = 0
for i in range(len(bin)):
pow = len(bin) - (i + 1)
dec += int(bin[i]) * (2**pow)
print("Equivalent decimal number : " ,dec)
OUTPUT :
Enter the number in binary : 1111
Equivalent decimal number : 15
For loop Programs in Python
Q19. 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
for i in range(1,num):
fact= fact*i
sum= sum + i/fact
print("Sum is : ",round(sum, 2))
OUTPUT :
Enter a any number : 3
Sum is : 2.0
Q20. Write a program to check whether a number is palindrome or not.
Ans.
num1 = input("Enter any number : ")
rnum = num1[: : -1]
if num1 == rnum:
print("Number is Palindrome")
else:
print("Number is not Palindrome")
OR
num1 = int(input("Enter any number : "))
onum = num1
r=0
rnum=0
len = len(str(num1))
for i in range(len):
r = num1 % 10
rnum = rnum * 10 + r
num1 = num1//10
if onum == rnum:
print("Number is Palindrome")
else:
print("Number is not Palindrome")
For loop Programs in Python
Q21. Write a python program to sum the sequence: 1 + 1/1! + 1/2! + 1/3! + …….. + 1/n!
Ans.
s=0
for i in range(10):
num = int(input("Enter number : "))
s = s + num
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
For loop Programs in Python
Q23. Write a program to accept 10 numbers from the user and display the largest & smallest number number.
Ans.
L = [ ]
for i in range(10):
num = int(input("Enter number : "))
L.append(num)
L.sort()
print("Largest number is : ", L[-1])
print("Smallest number is : ", L[0])
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
Largest number is : 10
Smallest number is : 1
Q24. 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)
Ans.
num1 = int(input("Enter first number"))
num2 = int(input("Enter second number"))
se = 0
so = 0
if num1 > num2 :
for i in range(num2, num1+1):
if i%2 == 0:
se = se + i
else:
so = so + i
else :
for i in range(num1, num2+1):
if i % 2 == 0:
se = se + i
else:
so = so + i
print("Sum of even numbers is : ", se)
print("Sum of odd numbers is : ", so)
OUTPUT:
Enter first number2
Enter second number7
Sum of even numbers is : 12
Sum of odd numbers is : 15
For loop Programs in Python
Q25. Write a program to display all the numbers which are divisible by 13 but not by 3 between 100 and 500.
Ans.
n = int(input("Enter value of n : "))
st = '2'
for i in range(n):
print(st, end=" , ")
st =st + '2'
OUTPUT :
Enter value of n : 4
2 , 22 , 222 , 2222 ,
For loop Programs in Python
Q27. 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 : "))
for i in range(1, n):
sum = sum + x**i/fac(i)
print(sum)
OUTPUT:
Enter value of n : 5
Enter value of x : 3
16.375
For loop Programs in Python
Q29. 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
for i in range(1, n+1):
sum = sum + 2**i/i
print(round(sum,2))
OUTPUT :
enter a nth term = 3
Enter value of x : 2
6.67
Q30. Write a program to find the sum of following series
Ans.
n = int(input("Enter number of terms : "))
s = 0
pr = 1
for i in range(1, n+1):
pr = i * pr
print(pr, end = " + ")
s = s + pr
print(" = ", s)
OUTPUT :
Enter number of terms : 5
1 + 2 + 6 + 24 + 120 + = 153
Q32. 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
for i in range(2, n+1):
if i % 2 == 0:
sp = sp + i ** 2
else :
sn = sn + i ** 2
print(sp - sn)
OUTPUT :
Enter number of terms : 6
23
For loop Programs in Python
Q33. Write a Program to print all the characters in the string ‘COMPUTER’ using for loop.
Ans.
n = int(input("Enter any number"))
print("Factors are : ")
for i in range(1, n+1):
if n % i == 0 :
print(i)OUTPUT :
Enter any number9
Factors are :
1
3
9
For loop Programs in Python
Disclaimer : I tried to give you the correct Solution of “For Loop Programs in Python” , but if you feel that there is/are mistakes in the solution of “For Loop Programs in Python“ given above, you can directly contact me at csiplearninghub@gmail.com.