Write a program in java to find the sum of the series
S = 1 + 2^2
/ a + 3^3
/ a2 + ...... to n terms
Answers
Answered by
5
Answer:
// Java program to find the sum of the given series
import java.util.Scanner;
public class HelloWorld {
// Function to return the sum of the series
public static float getSum(int a, int n)
{
// variable to store the answer
float sum = 0;
for (int i = 1; i <= n; ++i) {
// Math.pow(x, y) returns x^y
sum += (i / Math.pow(a, i));
}
return sum;
}
// Driver code
public static void main(String[] args)
{
int a = 3, n = 3;
// Print the sum of the series
System.out.println(getSum(a, n));
}
}
Similar questions
Accountancy,
2 months ago
Science,
2 months ago
English,
2 months ago
Math,
4 months ago
Geography,
10 months ago
Math,
10 months ago
India Languages,
10 months ago