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^2 + 1/2^2 + 1/3^2 …………1/n^2 in Java.

Somdeb Burman | 12:21:00 PM | 0 comments


import java.util.Scanner;
public class Series 
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter the no of terms ");
  int n = sc.nextInt();
  double sum = 0;
  for (int i = 1; i <= n; i++)
   sum = sum + (double) 1 / Math.pow(i, 2);
  System.out.println("Sum of series = " + sum);
 }
}

Output:
Enter the no of terms 
5
Sum of series = 1.4636111111111112
BUILD SUCCESSFUL (total time: 2 seconds)

Java Resources are available for Download from below

Category: , , , ,

0 comments