Program to find the sum of the series 1 + x + x ^ 2 + x ^ 3 + . . + x ^ n | Function to find the sum of the series and print N terms of the given series ; First Term ; Loop to print the N terms of the series and find their sum ; Driver code
def sum ( x , n ) : NEW_LINE INDENT total = 1.0 NEW_LINE multi = x NEW_LINE print ( 1 , end = " ▁ " ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT total = total + multi NEW_LINE print ( ' % .1f ' % multi , end = " ▁ " ) NEW_LINE multi = multi * x NEW_LINE DEDENT print ( ' ' ) NEW_LINE return total ; NEW_LINE DEDENT x = 2 NEW_LINE n = 5 NEW_LINE print ( ' % .2f ' % sum ( x , n ) ) NEW_LINE