Computer Science, asked by Mayb7186, 4 months ago

WAP to print the sum of the following series s=0+1+3+7+15+31+..... Upto n terms

Answers

Answered by anindyaadhikari13
1

Question:-

Write a program to print the sum of the following series.

S=0+1+3+7+15+31+.....N terms

Program:-

In Java

import java.util.*;

class Sum

{

public static void main(String args[])

{

int n, i, s=0, a=0;

Scanner sc=new Scanner(System.in);

System.out.print("Enter the value of N: ");

n=sc.nextInt();

for(i=1;i<=n;i++)

{

s=s+a;

a=a+i;

}

System.out.println("Sum of the series is: "+s);

}

}

In Python

n=int(input("Enter N: "))

s=a=0

for i in range(1,n+1):

s=s+a

a=a+i

print("Sum of the series is: ",s)

Similar questions