The document contains two Python programs: the first calculates the sum of the series 1 + x - x^2 - x^4 + ... + x^n, and the second prints Pascal's Triangle based on user-defined rows. The first program initializes the total and iteratively computes the series terms, while the second program uses nested loops to generate and print the triangle coefficients. Both programs demonstrate fundamental programming concepts such as loops and conditional statements.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
10 views2 pages
Week-3 Python Programs
The document contains two Python programs: the first calculates the sum of the series 1 + x - x^2 - x^4 + ... + x^n, and the second prints Pascal's Triangle based on user-defined rows. The first program initializes the total and iteratively computes the series terms, while the second program uses nested loops to generate and print the triangle coefficients. Both programs demonstrate fundamental programming concepts such as loops and conditional statements.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
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()