w3resource

Java: Print the first 15 numbers of the Pell series


First 15 Pell Numbers

Write a Java program to print the first 15 numbers of the Pell series.

In mathematics, the Pell numbers are an infinite sequence of integers. 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.:
thus, 70 is the companion to 29, and 70 = 2 × 29 + 12 = 58 + 12.
The first few terms of the sequence are :
0, 1, 2, 5, 12, 29, 70, 169, 408, 985, 2378, 5741, 13860,…

Sample Solution:

Java Code:

import java.util.Scanner;
public class Example25  {

    public static void main(String args[])
    {
	int n,a=1,b=0,c;
    System.out.println("First 20 Pell numbers: ");
    for(n=1; n<=20; n++)
     {
      c= a + 2*b;
      System.out.print(c+" ");
      a = b;
      b = c;
     }
   }
 }

Sample Output:

First 20 Pell numbers:                                                                                      
1 2 5 12 29 70 169 408 985 2378 5741 13860 33461 80782 195025 470832 1136689 2744210 6625109 15994428

Flowchart:

Flowchart: Print the first 15 numbers of the Pell series


For more Practice: Solve these Related Problems:

  • Write a Java program to compute Pell numbers recursively with memoization for efficiency.
  • Write a Java program to generate Pell numbers iteratively and then analyze the ratio between consecutive terms.
  • Write a Java program to display the first 15 Pell numbers using a while loop with formatted output.
  • Write a Java program to compare the performance of recursive and iterative methods for generating Pell numbers.

Go to:


PREV : Check Palindrome Number.
NEXT : Check Keith Number.

Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.