Welcome To The World Of Java

This site will teach you the basics of Java. It is not necessary to have any prior programming experience.

Print sum of series 1/1^3 – 1/2^3 + 1/3^3…….1/n^3 in Java.

Somdeb Burman | 11:47:00 AM | 0 comments


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: , , , ,

0 comments