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();
}
}
No comments:
Post a Comment