Week-3
3a) Write a program to calculate value of the following eries 1+x-x2-x4+….xn
SOURCE CODE:
# Python3 program to find sum of
# series of 1 + x^2 + x^3 + ....+x^n
# Function to find the sum of
# the series and print N terms
# of the given series
def sum(x, n):
total = 1.0
multi = x
# First Term
print(1, end = " ")
# Loop to print the N terms
# of the series and find their sum
for i in range(1, n):
total = total + multi
print('%.1f' % multi, end = " ")
multi = multi * x
print('\n')
return total;
# Driver code
x=2
n=5
print('%.2f' % sum(x, n))
3b)Write a program to print Pascal Triangle
SOURCE CODE:
rows = int(input("Enter number of rows: "))
coef = 1
for i in range(1, rows+1):
for space in range(1, rows-i+1):
print(" ",end="")
for j in range(0, i):
if j==0 or i==0:
coef = 1
else:
coef = coef * (i - j)//j
print(coef, end = " ")
print()