In the previous article we have discussed about Java Program to Print Series 7 14 21 28 35 42 …N
In this article we are going to see how to find sum of the series 1 + (1×2) + (1×2×3) + … + (1×2×3x…×n) by using Java programming language.
Java Program to Find the Sum of Series 1 + (1*2) + (1*2*3) + … + (1*2*3*4…*N)
On observing the pattern carefully, we can see
1st term = 1 (fixed)
Nth term = multiplication of n terms starting from 1
Example:
Suppose value of n = 3 1 + (1*2) + (1*2*3) = 1 + 2 + 6 = 9
Let’s see different ways to find sum of the series 1 + (1×2) + (1×2×3) + … + (1×2×3x…×n).
Method-1: Java Program to Find the Sum of Series 1 + (1*2) + (1*2*3) + … + (1*2*3*4…*N) By Using For Loop
Approach:
- Declare an integer variable say ‘
n
’ and assign the value to it, which holds the value of Nth term. - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Declare an long variable say ‘
sum
‘ and initialize it to 0. - Use a for loop from i =1 to i<=n (incremented by 1)
- Declare an long variable say ‘
term
‘ and initialize it to 1. - Inside the for loop, find the value of
term *= i
, and then add the value of term with sum for every iteration. - Print the result.
Program:
import java.util.*; public class Main { public static void main(String [] args) { //creating object of Scanner class Scanner s = new Scanner(System.in); //Taking input of number of elements in the series System.out.println("Enter the value of 'n' "); int n = s.nextInt(); //for loop to print the series long sum = 0, term = 1; for (int i = 1; i <= n; i++) { term *= i; sum += term; } System.out.println("Sum of the series is " + sum); } }
Output: Enter the value of 'n' 3 Sum of the series is 9
Method-2: Java Program to Find the Sum of Series 1 + (1*2) + (1*2*3) + … + (1*2*3*4…*N) By Using While Loop
Approach:
- Declare an integer variable say ‘
n
’ and assign the value to it, which holds the value of Nth term. - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Declare an long variable say ‘
sum
‘ and initialize it to 0. - Continue a while loop from i =1 to i<=n (incremented by 1)
- Declare an long variable say ‘
term
‘ and initialize it to 1. - Inside the while loop, find the value of
term *= i
, and then add the value of term with sum for every iteration. - Print the result.
Program:
import java.util.*; public class Main { public static void main(String [] args) { //creating object of Scanner class Scanner s = new Scanner(System.in); //Taking input of number of elements in the series System.out.println("Enter the value of 'n' "); int n = s.nextInt(); // while loop to print the series long sum = 0,term=1; int i = 1; while(i <= n) { term *= i; sum += term; i++; } System.out.println("Sum of the series is " + sum); } }
Output: Enter the value of 'n' 3 Sum of the series is 9
Method-3: Java Program to Find the Sum of Series 1 + (1*2) + (1*2*3) + … + (1*2*3*4…*N) By Using User Defined Method
Approach:
- Declare an integer variable say ‘
n
’ and assign the value to it, which holds the value of Nth term. - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Call a user defined method
printSeries()
by passingn
as parameter. - Inside method declare an long variable say ‘
sum
‘ and initialize it to 0. - Use a for loop from i =1 to i<=n (incremented by 1)
- Declare an long variable say ‘
term
‘ and initialize it to 1. - Inside the for loop, we will find the value of
term *= i
, and then add the value of term with sum for every iteration. - Print the result.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // creating object of scanner class Scanner s = new Scanner(System.in); //Taking input of number of elements in the series System.out.println("Enter the value of 'n' "); int n = s.nextInt(); // calling printSeries() method to print the series printSeries(n); } public static void printSeries(int n) { //for loop to print the series long sum = 0, term = 1; for (int i = 1; i <= n; i++) { term *= i; sum += term; } System.out.println("Sum of the series is " + sum); } }
Output: Enter the value of 'n' 4 Sum of the series is 33
Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.
Related Java Programs: