# 35 Important For loop Programs in Python (Solved) Class 11

Share with others

## d. First 10 Whole numbers

```Show AnswerAns.

a)

for i in range(2, 22, 2):
print(i)

b)

for i in range(1, 20, 2):
print(i)

c)

for i in range(1, 11):
print(i)

d)

for i in range(0, 10):
print(i)```

## 3 9 and so on

```Show AnswerAns.

print("Numbers\t\tSquares")
for i in range(1, 11):
print(i,"\t\t\t\t", i ** 2)

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.

for i in range(10, 301, 10) :
print(i, end = " , ")```

## 105, 98, 91 ………7

```Show AnswerAns.

for i in range(105, 6, -7) :
print(i, end = " , ")
```

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

```Show AnswerAns.

for i in range(10, 0, -1) :
print(i)```

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

```Show AnswerAns.

sum = 0
for i in range(1, 11, 1):
sum = sum + i
print(sum)```

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

```Show AnswerAns.

sum = 0
for i in range(2, 22, 2):
sum = sum + i
print(sum)```

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

```Show AnswerAns.

num = int(input("Enter any number"))
for i in range(1,11):
print(num," * ", i," = ", num *i)

OUTPUT :

Enter any number7
7  *  1  =  7
7  *  2  =  14
7  *  3  =  21
7  *  4  =  28
7  *  5  =  35
7  *  6  =  42
7  *  7  =  49
7  *  8  =  56
7  *  9  =  63
7  *  10  =  70```

## Q9. Write a program to display all even numbers that falls between two numbers (exclusive both numbers) entered from the user.

```Show AnswerAns.

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.

```Show AnswerAns.

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```

## 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
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.

```Show AnswerAns.

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```

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

```Show AnswerAns.

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

```Show AnswerAns.

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```

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

```Show AnswerAns.

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.

```Show AnswerAns.

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```

## 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
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.

```Show AnswerAns.

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
```

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

```Show AnswerAns.

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.

```Show AnswerAns.

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")```

## Q21. 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
for i in range(1, n + 1):
f = f * i
s = s + 1 / f
print("sum of sequence = ", s)```

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

```Show AnswerAns.

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```

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

```Show AnswerAns.

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)

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)

```Show AnswerAns.

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```

## Q25. Write a program to display all the numbers which are divisible by 13 but not by 3 between 100 and 500.

```Show AnswerAns.

for i in range(101, 500):
if i % 13 == 0 and i % 3 != 0:
print(i)```

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

```Show AnswerAns.

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 ,```

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

```Show AnswerAns.

n = int(input("Enter value of n : "))
for i in range(1, n+1):
print(i**2, end = " , ")

OUTPUT :

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

## 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 : "))
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```

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

```Show AnswerAns.

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

```

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

```Show AnswerAns.

n = int(input("Emter number of terms  : "))
s = 0
for i in range(1, n+1):
s = s + i ** 3
print(s)

OUTPUT :

Emter 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
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```

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

```Show AnswerAns.

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```

## Q33. Write a Program to print all the characters in the string ‘COMPUTER’ using for loop.

```Show AnswerAns.

str = "COMPUTER"
for i in str:
print(i)

OUTPUT :

C
O
M
P
U
T
E
R```

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

```Show AnswerAns.

L = [23, 45, 32, 25, 46, 33, 71, 90]
for i in L :
if i % 2 != 0 :
print(i)

OUTPUT :
23
45
25
33
71```

## Q35. 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 : ")
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.

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
error: Content is protected !!