Computer Science, asked by jerefreddy, 2 months ago

Write a java program to find the sum of the following series.​

Attachments:

Answers

Answered by kamalrajatjoshi94
3

Answer:

Program:-

import java.util.*;

public class Main

{

public static void main(String args[])

{

Scanner in=new Scanner(System.in);

int x,i,n,a=1;

double s=0;

System.out.println("Enter the value of x:");

x=in.nextInt();

System.out.println("Enter limit");

n=in.nextInt();

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

{

if(i%2==0)

s=s-Math.pow(x,a);

else

s=s+Math.pow(x,a);

a=a+2;

}

System.out.println("The sum of the series:"+s);

in.close();

}

}

Logic:-

Run a loop if I is even it prints the sum negative else positive and also declare a separate variable for the powers which gets incremented by 2 after each increment so it prints the output.

Prove for the output:-

 {2}^{1}  -  {2}^{3}  +  {2}^{5}  -  {2}^{7}

2-8+32-128

= -136+24

= -102

Attachments:
Similar questions