In the previous article we have discussed about Java Program to Print Series 5 25 125 625 3125…N
In this article we are going to see how to print the series 2 5 10 17 26 37 50… N by using Java programming language. Along with 2 5 10 17 series in java, series program in java, 2 5 10 17 Pattern, Number Series Java Program, Print Series In Java, Java Series Program Examples.
Java Program to Print Series 2 5 10 17 26 37 50…N
On observing the pattern carefully, you can see this series is in in the form of i*i+1
where i
starts from 1 and goes on up to N.
When i=1: i*i+1 = 1*1+1 = 2
When i=2: i*i+1 = 2*2+1 = 5
When i=3: i*i+1 = 3*3+1 = 10 and so on.
So the series is like 2 5 10 17 … N
Example:
When value of N = 5 Then series: 2 5 10 17 26
Let’s see different ways to print the series 0 3 8 15 24 35 48…N
Method-1: Java Program to Print Series 2 5 10 17 26 37 50…N By Using For Loop
Approach:
- Declare an integer variable say ‘
n
’ which holds the Nth term of the series. - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Let declare a integer variable ‘
result
‘ - Use a for loop from
i=1 to i<=n
incremented by 1 - Inside for loop find the value of
result = i*i+1
- 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 result; //for loop to print the series for (int i = 1; i <= n; i++) { result=i*i+1; System.out.print(" "+result); } } }
Output: Enter the Nth term “N” 5 2 5 10 17 26
Method-2: Java Program to Print Series 2 5 10 17 26 37 50…N By Using While Loop
Approach:
- Declare an integer variable say ‘
n
’ which holds the Nth term of the series. - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Let declare a integer variable ‘
result
‘ - Declare and initialize an integer variable
i=1
- Continue a while loop till
i<=n
, wherei
incremented by 1. - Inside while loop find the value of
result = i*i+1
- Print the result in the series.
Program:
import java.util.*; public class Main { public static void main(String [] args) { 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 result; int i=1; while(i<=n) { result=i*i+1; System.out.print(" "+result); i++; } } }
Output: Enter the Nth term “N” 5 2 5 10 17 26
Method-3: Java Program to Print Series 2 5 10 17 26 37 50…N By Using User Defined Method
Approach:
- Declare an integer variable say ‘
n
’ which holds the Nth term of the series - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Call a user defined method
printSeries()
by passing n as parameter. - Inside method declare a integer variable ‘
result
‘ - Use a for loop from
i=1 to i<=n
incremented by 1 - Inside for loop find the value of
result = i*i+1
- 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 vale of Nth term “N”"); int n = s.nextInt(); // calling printSeries method to print the series printSeries(n); } //user defined method printSeries() to print the series public static void printSeries(int n) { int result; //for loop to print the series for (int i = 1; i <= n; i++) { result=i*i-1; System.out.print(" "+result); } } }
Output: Enter the vale of Nth term “N” 5 2 5 10 17 26
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Recommended Reading On: Java Program to Print Series 10 20 30 40 40 50 …N
Try these:
- Series Question In Java
- Number Series Questions In Java
- 2 5 10 15 26 37
- 2, 5, 10, 15, 26, 37
- 26+37
- Java Print
- 2-4+6-8+10 Series In Java
Related Java Programs: