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,
1 month ago
Physics,
1 month ago
Computer Science,
2 months ago
Chemistry,
2 months ago
Social Sciences,
9 months ago
Math,
9 months ago
Math,
9 months ago