Sum of Series Program in Java

Sum of Series Program in Java | Number Series Program in Java | There are lots of series programs are there, we will write a Java program for some of them. After observing these codes you can easily write the program for another sum of series. In another post, we have also written the Fibonacci series program in Java,  and Fibonacci Series using Recursion in Java.

Quick navigation,

1. Java Sum of Series program for 1+2+3+4+5+..+N

The series 1+2+3+4+5+..+N is the sum of first N natural numbers. To find the sum of this series we have to take the help of loop. 

// series:- 1 + 2 + 3 + 4 + ... + N
import java.util.Scanner;
public class Series {
  public static void main(String[] args) {

    // create Scanner class object
    // to read input values
    Scanner scan = new Scanner(System.in);

    // declare variables
    int n;
    int sum = 0;

    // read N value
    System.out.print("Enter N value: ");
    n = scan.nextInt();

    // calculate the sum of series
    for(int i=1; i<=n; i++) {
      sum = sum + i;
    }

    // display result
    System.out.println("Sum = " + sum);
  }
}

Output for the different test-cases:-

Enter N value: 5
Sum = 15

Enter N value: 10
Sum = 55

In time the complexity of this program is O(n). But there is one more easy to calculate the sum of first N natural numbers using the formula:- N(N-1)/2 and the time complexity will be O(1).

public class Series {
  public static void main(String[] args) {

    int n = 5;
    int sum = 0;

    // calculate the sum of series
    sum = n*(n-1)/2;

    // display result
    System.out.println("Sum = " + sum);
  }
}

Output:-

Sum = 10

2. Series:-12 + 22 + 32 + 42 + 52 +….+ n2

The series 12 + 22 + 32 + 42 + 52 +….+ n2 or 1*1 + 2*2 + 3*3 + 4*4 + 5*5 +….+ n*n is the sum of square of first N natural numbers. It is similar to the previous series but we have to add square value of the numbers.

// series:- 1*1 + 2*2 + 3*3 + 4*4 + 5*5 +….+ n*n
import java.util.Scanner;
public class Series {
  public static void main(String[] args) {

    // create Scanner class object
    // to read input values
    Scanner scan = new Scanner(System.in);

    // declare variables
    int n;
    int sum = 0;

    // read N value
    System.out.print("Enter N value: ");
    n = scan.nextInt();

    // calculate the sum of series
    for(int i=1; i<=n; i++) {
      sum = sum + i*i;
    }

    // display result
    System.out.println("Sum = " + sum);
  }
}

Output for the different test-cases:-

Enter N value: 5
Sum = 55

Enter N value: 10
Sum = 385

The time complexity of this program is O(n), but we can use a simple formula as sum = n*(n+1)*(2*n+1)/6, which is having a time complexity of O(1). Let us see it.

public class Series {
  public static void main(String[] args) {

    int n = 5;
    int sum = 0;

    // calculate the sum of series
    sum = n*(n+1)*(2*n+1)/6;

    // display result
    System.out.println("Sum = " + sum);
  }
}

Output:-

Sum = 55

3. 1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 +……+ 1/N

In this program, while finding the sum value, we have to divide 1 by the number (or iteration variable). The values of 1/2, 1/3 will be in floating-point numbers therefore to store them sum values take floating-point data types (either float or double).

// series:- 1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 +……+ 1/N
import java.util.Scanner;
public class Series {
  public static void main(String[] args) {

    // create Scanner class object
    // to read input values
    Scanner scan = new Scanner(System.in);

    // declare variables
    int n;
    double sum = 0.0;

    // read N value
    System.out.print("Enter N value: ");
    n = scan.nextInt();

    // calculate the sum of series
    for(int i=1; i<=n; i++) {
      sum = sum + (1 / (double)i);
    }

    // display result
    System.out.println("Sum = " + sum);
  }
}

Output for the different test-cases:-

Enter N value: 5
Sum = 2.283333333333333

Enter N value: 10
Sum = 2.9289682539682538

Enter N value: 50
Sum = 4.499205338329423

The iterator variable “i” is of the int data type. If we divide 1/i then we will always get 0 because results will be between 0 to 1 which is a floating-point value. To get the correct result, typecast the iterator variable “i” while calculating the sum value. The expression 1/(double)I will give the result in floating-point value, but the expression 1/i will give the result in an integer value.

4. Series:- 1 + 1/(2*2) + 1/(3*3) + 1/(4*4) + ….. + 1/(n*n)

This series is similar to the previous series, just divide the 1 by square value of the iterator variable (i.e. i*i). Similar to the previous program use typecasting in the program. For a given value of N, this program will always give a lesser value compared to the previous program.

// series:- 1 + 1/(2*2) + 1/(3*3) + 1/(4*4) + ….. + 1/(n*n)
import java.util.Scanner;
public class Series {
  public static void main(String[] args) {

    // create Scanner class object
    // to read input values
    Scanner scan = new Scanner(System.in);

    // declare variables
    int n;
    double sum = 0.0;

    // read N value
    System.out.print("Enter N value: ");
    n = scan.nextInt();

    // calculate the sum of series
    for(int i=1; i<=n; i++) {
      sum = sum + (1 / (double)(i*i));
    }

    // display result
    System.out.println("Sum of Series = " + sum);
  }
}

Output:-

Enter N value: 5
Sum of Series = 1.4636111111111112

Enter N value: 10
Sum of Series = 1.5497677311665408

Enter N value: 50
Sum of Series = 1.625132733621529

5. Series:- 1/1! + 2/2! + 3/3! + …… + N/N!

To write a Java program for the sum of series 1/1! + 2/2! + 3/3! + …… + N/N!, we must know how to find the factorial value of a number in Java. We will use the loop and the 1 will be divided by the factorial value of the iterator variable (i.e. i!). Now, let us see the Java program for this sum of series,

// series:- 1/1! + 2/2! + 3/3! + …… + N/N!
import java.util.Scanner;
public class Series {
  public static void main(String[] args) {

    // create Scanner class object
    // to read input values
    Scanner scan = new Scanner(System.in);

    // declare variables
    int n;
    long factorial = 0;
    double sum = 0.0;

    // read N value
    System.out.print("Enter N value: ");
    n = scan.nextInt();

    // calculate the sum of series
    for(int i=1; i<=n; i++) {
      factorial = findFactorial(i);
      sum = sum + (i / (double)factorial);
    }

    // display result
    System.out.println("Sum of Series = " + sum);
  }

  // recursive Java method to 
  // find factorial of a number
  // using if-else statement
  public static long findFactorial(int number) {
    if(number == 0) return 1;
    else 
    return number*findFactorial(number-1);
  }
}

Output for the different test-cases:-

Enter N value: 5
Sum of Series = 2.708333333333333

Enter N value: 10
Sum of Series = 2.7182815255731922

Enter N value: 50
Sum of Series = 2.718281828459045

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *