write a program in java to find the sum of the series 1+(2/x^2)+(3/x^3)+(4/x^4)+.......to n terms.
Answers
Series Summation in Java
We are given a series with a variable x.
We would take user input for the value of x and the number of terms n. Then we would be finding the series sum using a loop.
The term of the series can be written as .
Only the first term does not follow the pattern. So we create and initialize a variable (here called Sum) to 1 and then iterate through a loop to add the successive terms.
Here's a Java Program for it:
import java.util.Scanner; //Importing Scanner
public class Brainly //Creating class
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); //Creating Scanner object
System.out.print("Enter the value of x: ");
double x = sc.nextDouble(); //User input for x
System.out.print("Enter number of terms: ");
int n = sc.nextInt(); //User input for n
double Sum = 1; //Creating and Initialising Sum to 1 (the first term)
for(int i=2;i<=n;i++) //Setting a loop to run from 2 to n
{
Sum += i/(Math.pow(x,i)); //Adding the terms
}
System.out.println("Series Sum = "+Sum); //Printing Sum
}
}