Write a java program to find the sum of the following series.
Attachments:
Answers
Answered by
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-8+32-128
= -136+24
= -102
Attachments:
Similar questions
Math,
21 days ago
Chemistry,
21 days ago
English,
21 days ago
Computer Science,
1 month ago
Chemistry,
1 month ago
Social Sciences,
8 months ago
Math,
8 months ago
Math,
8 months ago