In the previous article we have discussed about Java Program to Find the Sum of Series 1 + (1*2) + (1*2*3) + … + (1*2*3*4…*N)
In this article we are going to see how to print the sum of the series 1 + (1+2) + (1+2+3) + … + (1+2+3+4…+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 = sum of n terms starting from 1
Example:
Suppose value of n = 3 1 + (1+2) + (1+2+3) = 1 + 3 + 6 = 10
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 0. - 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(); //for loop to print the series long sum = 0, term = 0; 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 10
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. - Use a while loop
from i=1 to i<=n
(incremented by 1) - Declare an long variable say ‘
term
‘ and initialize it to 0. - Inside while 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(); // while loop to print the series long sum = 0,term = 0; 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 10
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 say
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 0. - 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 = 0; 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 20
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
Related Java Programs: