Saturday, August 31, 2019

BlueJ Program To Display Sum of Series: x – x^3 + x^5 – x^7 + ….. x^n


This is a BlueJ program which displays the sum of a series using pow function of Math class.


import java.util.*;
class Max
{
Scanner sc=new Scanner(System.in);
int n,i,x,c=1;
double sum=0;
public void take()
{
System.out.print("\nValue of 'n': ");


n=sc.nextInt();
System.out.print("\nValue of 'x': ");
x=sc.nextInt();
for(i=1;i< =n;i=i+2)
{
  sum=sum + Math.pow(x,i)*c;
  c=c*-1;
}
System.out.println("\nSum of the series: "+sum);
}
public static void main(String args[])
{
Max ob=new Max();
ob.take();
}
}


Back To BlueJ Series Display Programs Home Page: CLICK HERE

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner