Class – XI Computer Science (083)
Assignment – 4
Topic : Nested loops
Q.49. Write a program to print the sum of the following series:-
Program:-
n = int(input("How many terms?"))
Output:-
x = int(input("x?")) How many terms?5
x?2
s=1 Sum of series= 61
for i in range(2,n+1):
s += x**i
print("Sum of series=",s)
Q.50. Write a program using nested loops to print this pascal’s triangle:- (nested while
loop)
Program:-
i=1
while i < 5:
j=1 Output:-
while j < 2*i: 1
1 3
print(j,end=' ') 1 3 5
1 3 5 7
j += 2
print()
i += 1
Q.51. Numbers in the form 2𝑛 − 1 are called Mersenne Numbers, e.g., 21 − 1 = 1, 22 −
1 = 3, 23 − 1 = 7. Write a Python script that displays first ten Mersenne numbers.
Program:-
print("First 10 Merenne numbers...")
for i in range(1,11):
print(2**i-1)
Output:-
First 10 Merenne numbers...
15
31
63
127
255
511
1023
Q.52. Write a program to print the sum of the following series:-
Program:-
n = int(input("How many terms?"))
Output:-
series_sum = 0 How many terms?4
Sum of series= 20
for i in range(1,n+1):
s=0
for j in range(1,i+1):
s += j
series_sum += s
print("Sum of series=",series_sum)
Q.53. Write a python program to print the following pascal’s triangle:- (nested for loop)
Output:-
Program:-
4 3 2 1
4 3 2
for i in range(1,5): 4 3
4
for j in range(4,i-1,-1):
print(j,end=' ')
print()
Q.54. Write a short program to print the following series:-
Program:-
(i) Output:-
for i in range(1,41,3): 1 4 7 10 13 16 19 22 25 28 31 34 37 40
print(i,end=' ')
(ii)
for i in range(1,41,3): Output:-
sign = (-1)**(i+1) 1 -4 7 -10 13 -16 19 -22 25 -28 31 -34 37 -40
print(i*sign,end=' ')
Q.55. Write a program to ask for n numbers from the user and print the factorial of each
of the numbers.
Expected output:-
How many nos?5
Enter a number:4
Factorial of 4 = 24
Enter a number:6
Factorial of 6 = 720
Enter a number:3
Factorial of 3 = 6
Enter a number:7
Factorial of 7 = 5040
Enter a number:5
Factorial of 5 = 120
Program:-
n = int(input("How many numbers?"))
for i in range(1,n+1):
num = int(input("Enter a number:"))
fact = 1
for j in range(1,num+1):
fact *= j
print("Factorial of",num,"=",fact)
Q.56.
Program:-
(a)
Output:-
n = int(input("How many terms?"))
How many terms?5
num,den,s = 2,9,0
Sum of series= 0.34438554909143143
for i in range(1,n+1):
sign = (-1)**(i+1)
s += num/den * sign
num += 3
den += 4
print("\nSum of series=",s)
(b)
n = int(input("How many terms?"))
Output:-
s=0
How many terms?5
for i in range(1,2*n+1,2):
Sum of series= 165
s += i**2
print("Sum of series=",s)
Q.57.
Program:-
n = int(input("How many terms?"))
s=0
for i in range(n): Output:-
f=1 How many terms?5
for j in range(1,i+1): sum of series= 2.708333333333333
f *= j
s += 1/f
print("sum of series=",s)
Q.58.
Program:-
(a)
n = int(input("How many terms?"))
x = int(input("x??"))
s=0 Output:-
for i in range(1,n): How many terms?5
f=1 x??2
for j in range(1,i+1): sum of series= 0.6666666666666666
f *= j
sign = (-1)**(i+1)
s += x**i / f * sign
print("sum of series=",s)
(b)
n = int(input("How many terms?")) Output:-
x = int(input("x??")) How many terms?5
s=0 x??2
for i in range(1,n): sum of series= 10.666666666666666
s += x**i / i
print("sum of series=",s)
Q.59.
Program:-
(a)
Output:-
ascii = 65
A
for i in range(1,7): A B
A B C
for j in range(ascii,ascii+i): A B C D
A B C D E
print(chr(j),end=' ') A B C D E F
print()
(b)
Output:-
ascii = 65
A
for i in range(1,6): B B
C C C
for j in range(i): D D D D
E E E E E
print(chr(ascii+i-1),end=' ')
print()
(c)
Output:-
for i in range(1,6):
0
for j in range(i): 2 2
4 4 4
print(2*(i-1),end=" ") 6 6 6 6
8 8 8 8 8
print()
(d)
Output:-
for i in range(5):
2
for j in range(i): 4 4
6 6 6
print(2*i,end=" ") 8 8 8 8
print()
Q.60.
Program:-
for i in range(1,7): Output:-
* * * * * * * * * * * * * * * * * * * *
for j in range(1,21): * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
print('*',end=' ') * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
print() * * * * * * * * * * * * * * * * * * * *