JAVA-Sum of Series

Q. Write a program in JAVA to find the sum of the following series:
a sin x + a 2 cos x + a 3 sin x + a 4 cos x + … Take the required values from user.
import java.util.Scanner;
import java.lang.Math;
class series
{
public static void main(String args[])
{
int a,x,n;
Scanner sc=new Scanner(System.in);
System.out.print("eneter the value of a= ");
a=sc.nextInt();
System.out.print("eneter how many elements of series= ");
n=sc.nextInt();
System.out.print("eneter the value of x= ");
x=sc.nextInt();
for(i=0;i<n;i++)
{
if(i%2!=0)
s=s+Math.pow(a,i)*Math.sin(x);
else
s=s+Math.pow(a,i)*Math.cos(x);
}
System.out.print("sum="+s);
}
}
Output:
enter the value of a= 3
enter how many elements of series= 2
enter the value of  x= 0
sum= 9.0
//you can also do it using Buffered Reader Class, to see the implementation of Buffered Reader Class go to prevoius posts

/*if you have any problem, comment below*/




Comments