In the previous article we have discussed about Java Program to print Series 5 6 9 14 21 … N
In this article we are going to see how to print the series 1 3 6 11 18 … N by using Java programming language.
Java Program to Print the Series 1 3 6 11 18 … N
On observing the pattern carefully, we can see 1st number starts from 1
Then the next number is the addition of prime numbers in sequence. Like Previous Element + Next Prime Number
Example:
1 | 3 | 6 | 11 | 18 | 29 | ….… | N |
+2 +3 +5 +7 +11
Let’s see different ways to print the series 1 3 6 11 18 … N
Method-1: Java Program to Print the Series 1 3 6 11 18 … N By Using For 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 a integer variable say ‘
result
’ and initialize it to 1 - Use a for loop from
i=0 to result + i<=n
where the loop is incremented by 1 - Inside the for loop we will call a
isPrime()
boolean method. - Inside
isPrime()
method we will find the prime number using a for loopfrom j=2 to j<=i
and incremented by 1 - Inside the for loop if
i%j == 0
then it returns false to themain()
. - If the
isPrime()
returns true then the value of result inmain()
is equal toresult+i
- Print the result in the series.
Program:
import java.util.Scanner; import java.io.*; 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 limit "); int n = s.nextInt(); int result = 1; System.out.print(result); //for loop to print the series for (int i = 0; result + i <= n; i++) { // calling isPrime() method to find the prime number if (isPrime(i)) { result += i; System.out.print(" " + result); } } } // isPrime() method to find the prime number static boolean isPrime(int i) { if (i == 1 || i == 0) return false; for (int j = 2; j < i; j++) { if (i % j == 0) return false; } return true; } }
Output: Enter the limit 100 1 3 6 11 18 29 42 59 78
Method-2: Java Program to Print the Series 1 3 6 11 18 … 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 a integer variable say ‘
result
’ and initialize it to 1 - Declare and initialize an integer variable
i=1
- Continue a while loop
till result + i<=n
, wherei
is incremented by 1. - Inside the while loop we will call
isPrime()
boolean method. - Inside
isPrime()
method we will find the prime number using a while loopfrom j=2 to j<=i
and incremented by 1 - Inside the while loop If
i%j == 0
then it returns false to themain()
. - If the
isPrime()
returns true then the value of result in main() is equal toresult+i
- Print the result in the series.
Program:
import java.util.Scanner; import java.io.*; 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 limit "); int n = s.nextInt(); int result = 1; System.out.print(result); //while loop to print the series int i = 0; while(result + i <= n) { // calling isPrime() method to find the prime number if (isPrime(i)) { result += i; System.out.print(" " + result); } i++; } } // isPrime() method to find the prime number static boolean isPrime(int i) { if (i == 1 || i == 0) return false; int j = 2; while( j < i) { if (i % j == 0) return false; j++; } return true; } }
Output: Enter the limit 500 1 3 6 11 18 29 42 59 78 101 130 161 198 239 282 329 382 441
Method-3: Java Program to Print the Series 1 3 6 11 18 … N By Using User Defined Method
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
. - Then call a user defined method
printSeries()
by passingn
as parameter. - Let declare a integer variable say ‘
result
’ and initialize it to 1 - Use a for loop from
i=0 to result + i<=n
where the loop is incremented by 1 - Inside the for loop we will call a
isPrime()
boolean method. - Inside
isPrime()
method we will find the prime number using a for loopfrom j=2 to j<=i
and incremented by 1 - Inside the for loop if
i%j == 0
then it returns false to themain()
. - If the
isPrime()
returns true then the value of result inmain()
is equal toresult+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 limit "); 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=1; System.out.print(result); //for loop to print the series for (int i = 0; result + i <= n; i++) { // calling isPrime() method to find the prime number if (isPrime(i)) { result += i; System.out.print(" " + result); } } } // isPrime() method to find the prime number static boolean isPrime(int i) { if (i == 1 || i == 0) return false; for (int j = 2; j < i; j++) { if (i % j == 0) return false; } return true; } }
Output: Enter the limit 1000 1 3 6 11 18 29 42 59 78 101 130 161 198 239 282 329 382 441 502 569 640 713 792 875 964
Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples.
Related Java Programs: