﻿ Java exercises: Print the first 15 numbers of the Pell series - 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: Java Code Editor:

What is the difficulty level of this exercise?

﻿

## Java: Tips of the Day

Array vs ArrayLists:

The main difference between these two is that an Array is of fixed size so once you have created an Array you cannot change it but the ArrayList is not of fixed size. You can create instances of ArrayLists without specifying its size. So if you create such instances of an ArrayList without specifying its size Java will create an instance of an ArrayList of default size.

Once an ArrayList is full it re-sizes itself. In fact, an ArrayList is internally supported by an array. So when an ArrayList is resized it will slow down its performance a bit as the contents of the old Array must be copied to a new Array.

At the same time, it's compulsory to specify the size of an Array directly or indirectly while creating it. And also Arrays can store both primitives and objects while ArrayLists only can store objects.

Ref: https://bit.ly/3o8L2KH