WAP to print the sum of the following series S=x^2-x^4+x^6-x^8 up to n terms
Answers
Question:-
Write a program to print the sum of the series.
S=x^2 - x^4 + x^6 - x^8...N terms.
Program:-
class Program
{
public static void main(String args[])
{
int n=10, x=5, a=2;
double s=0.0;
for(int i=1;i<=n;i++)
{
if(i%2==1)
s+=Math.pow(x, a);
else
s-=Math.pow(x, a);
a=a+2;
}
System.out.println("Sum of the series: "+s);
}
}
Explanation:
Java program to get the sum of the series
import java.io.*;
class MathSeries {
// Function to get the series
static double Series(double x, int n)
{
double sum = 1, term = 1, fct, j, y = 2, m;
// Sum of n-1 terms starting from 2nd term
int i;
for (i = 1; i < n; i++) {
fct = 1;
for (j = 1; j <= y; j++) {
fct = fct * j;
}
term = term * (-1);
m = Math.pow(x, y) / fct;
m = m * term;