Print sum of series 1/1^3 – 1/2^3 + 1/3^3…….1/n^3 in Java.
import java.util.Scanner;
public class Series
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
double sum = 0;
System.out.println("Enter the no of terms ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++)
{
if (i % 2 == 0) {
sum = sum - (double) 1 / (i * i * i);
} else {
sum = sum + (double) 1 / (i * i * i);
}
}
System.out.println(" Sum of the series : " + sum);
}
}
Output:
Enter the no of terms
5
Sum of the series : 0.904412037037037
BUILD SUCCESSFUL (total time: 3 seconds)
Java Resources are available for Download from below
Category: Core Java, Interview Question, Programs, Seriers, Written Test
0 comments