Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Java Exercises: Print the first 15 numbers of the Pell series

Java Numbers: Exercise-25 with Solution

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

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java program to check if a number is palindrome or not.
Next: Write a Program in Java to check whether a number is a Keith Number or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Java: Tips of the Day

How to sort an ArrayList?

Collections.sort(testList);
Collections.reverse(testList);

That will do what you want. Remember to import Collections though!

Ref: https://bit.ly/32urdSe