Java: 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:
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/java-exercises/numbers/java-number-exercise-25.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics