In the previous article we have discussed about Java Program to Find the Sum of Series 1!/1 + 2!/2 + 3!/3 + 4!/4+ 5!/5 + N!/N
In this article we are going to see how to print the series 1+ 2 + 3 + 4 + … + N by using Java programming language.
Java Program to Print the Series 1+ 2 + 3 + 4 + … + N
On observing the pattern carefully, we can see
First, number starts from 1, then the next term is incremented by 1 with the previous term.
And finally all the terms are added.
Example:
n = 5
1+2+3+4+5 = 15
Alternative, we can directly use the formula as (n*(n+1))/2
n = 5
(n*(n+1))/2 = 5*6/2 = 15
1 | 2 | 3 | 4 | 5 | 6 | …… | N |
+1 +1 +1 +1 +1
Let’s see different ways to
Method-1: Java Program to Print the Series 1+ 2 + 3 + 4 + … + N By Using For Loop
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘
n
’ which holds the number of terms in series. - Prompt the user to enter a number as value of
n
. - Let declare an integer variable say ‘
sum
’ and initialize it to 0. - Use a for loop
from i=1 to i<=n
where the loop is incremented by 1. - Inside the for loop we will find the value of
sum=sum+i
- Print the result in the series.
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 Nth term “N” "); int n = s.nextInt(); int sum = 0; //for loop to print the series for (int i = 1; i <= n; i++) { sum+=i; } System.out.print("The sum of the series is "+sum); } }
Output: Enter the Nth term “N” 5 The sum of the series is 15
Method-2: Java Program to Print the Series 1+ 2 + 3 + 4 + … + N By Using While Loop
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘
n
’ which holds the Nth term of the series. - Prompt the user to enter a number as value of
n
. - Let declare integer variable say ‘
sum
’ and initialize it to 0 - Declare and initialize an integer variable
i=1
- Continue a while loop
till i<=n
, wherei
is incremented by 1. - Inside while loop find the value of
sum+=i
- Print the result in the series.
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 Nth term “N” "); int n = s.nextInt(); int sum = 0; int i=1; while(i<=n) { sum +=i; i++; } System.out.print("The sum of the series is "+sum); } }
Output: Enter the Nth term “N” 7 The sum of the series is 28
Method-3: Java Program to Print the Series 1+ 2 + 3 + 4 + … + N By Using User Defined Method
Approach:
- The same logic as method 1 but this time we are moving the for inside a user-defined method.
- Create a Scanner class object.
- Prompt the user to enter a value for
n
which is the number of terms in series. - Call a user defined method
printSeries()
by passingn
as parameter. - Inside the method we will use direct formula as
n*(n+1)/2
to find the sum series. - Print the result in the series.
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 Nth term 'N' "); int n = s.nextInt(); // calling printSeries method to print the series printSeries(n); } //printSeries metthod to print the series public static void printSeries(int n) { int result = n*(n+1)/2; System.out.print("The sum of the series is "+result); } }
Output: Enter the value of Nth term 'N' 9 The sum of the series is 45
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.
Related Java Programs: