Java Program to Print Pell Number Series 0 1 2 5 12 29 70 …N

In the previous article, we have discussed about Java Program to Print Triangular Number Series 1 3 6 10 15 …N

In this article we are going to see how to print Pell number series 0 1 2 5 12 29 70 …N by using Java programming language. What is pell series in java, Pell Number In Java, 1 2 5 12 29 Series to print pell program, Number Series Program In Java, Number Series In Java.

Java Program to Print Pell Number Series 0 1 2 5 12 29 70 …N

On observing the pattern carefully, we can see the numbers in the series are pell number.

The sequence of Pell numbers starts with 0 and 1, and then each Pell number is the sum of twice the previous Pell number and the Pell number before that.

Example:

Initial = 0, 1

Next no = 2*1 + 0 = 2

Next no = 2*2 +1 = 5

Next no = 2*5 + 2 = 12

Next no= 2*12 + 5 = 29

Let’s see different ways to print Pell number series 0 1 2 5 12 29 70 …N

Method-1: Java Program to Print Pell Number Series 0 1 2 5 12 29 70 …N By Using For Loop

Approach: 

  • Declare an int variable say ‘n’ and assign the value to it, which holds the nth value of the series
  • Create Scanner class object.
  • Prompt the user to enter a number as value of n.
  • Use a for loop from i=1 to i<=n
  • Inside for loop find each term by using concept of Pell number.
  • Print the result.

Program:

import java.util.*;
public class Main
{
   public static void main(String[] args)
   {
        //Scanner class object created
        Scanner s = new Scanner(System.in);
        //taking input of 'n' value from user
        System.out.println("Enter the nth value of the series:");
        int n=s.nextInt();
        int a = 0, b=1;
        for(int i = 1; i<=n; i++)
        {
            int c = b + 2*a;
            b=a;
            a=c;
            System.out.print(c+ ",  ");
        }
   }
}
Output:

Enter the nth value of the series:
10
1, 2, 5, 12, 29, 70, 169, 408, 985, 2378,

Method-2: Java Program to Print Pell Number Series 0 1 2 5 12 29 70 …N By Using While Loop

Approach: 

  • Declare an int variable say ‘n’ which holds the nth value of the series.
  • Create Scanner class object.
  • Prompt the user to enter a number as value of n.
  • Continua a a while loop from i=1 till i<=n
  • Inside for loop find each term by using concept of Pell number.
  • Print the result

Program:

import java.util.*;
public class Main
{
   public static void main(String[] args)
   {
        //Scanner class object created
        Scanner s = new Scanner(System.in);
        //taking input of 'n' value from user
        System.out.println("Enter the nth value of the series:");
        int n=s.nextInt();
        int a = 0, b=1;
        // using while loop to print the series
        int i = 1;
        while( i<=n)
        {
            int c = b + 2*a;
            b=a;
            a=c;
            System.out.print(c+ ",  ");
            i++;
    	}
   }
}
Output:

Enter the nth value of the series:
10
1, 2, 5, 12, 29, 70, 169, 408, 985, 2378,

Method-3: Java Program to Print Pell Number Series 0 1 2 5 12 29 70 …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 Scanner class object.
  • Prompt the user to enter a number as value of n.
  • Use a for loop from i=1 to i<=n
  • Inside for loop find each term by using concept of Pell number.
  • Print the result.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
   {
        //Scanner class object created
        Scanner s = new Scanner(System.in);
        //taking input of 'n' value from user
        System.out.println("Enter the nth value of the series:");
        int n=s.nextInt();
        m1(n);
   }
    public static void m1(int n)
    {
        int a = 0, b=1;
        for(int i = 1; i<=n; i++)
        {
            int c = b + 2*a;
            b=a;
         a=c;
         System.out.print(c+ ",  ");
        }

    }
}
Output:

Enter the nth value of the series:
10
1, 2, 5, 12, 29, 70, 169, 408, 985, 2378,

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.

Recommended Reading On: Python Program to Generate first N numbers of Pell Series

Try these for sample to know the knowledge of yourself on this topic:

  1. – ‘0’ In Java
  2. 0′ In Java
  3. N 2 Java
  4. 1,2,5,12,29
  5. What
  6. 1, -2, 5, -12, 29, -70, 169,
  7. What Is Pell Series
  8. Print Series In Java
  9. How To Print Series In Java
  10. Print The Series In Java
  11. Series Program In Java

Related Java Programs: