C++ Exercises: Print the first 20 numbers of the Pell series
C++ Numbers: Exercise-39 with Solution
Write a program in C++ to print the first 20 numbers of the Pell series.
Sample Solution:
C++ Code :
#include <iostream>
using namespace std;
int main()
{
int n,a=1,b=0,c;
cout << "\n\n Find the first 20 numbers of the Pell series: \n";
cout << " --------------------------------------------------\n";
cout<<" The first 20 numbers of Pell series are: "<<endl;
c=0;
cout<<" "<<c<<" ";
for(n=1; n<20; n++)
{
c= a + 2*b;
cout<<c<<" ";
a = b;
b = c;
}
cout<<endl;
}
Sample Output:
Find the first 20 numbers of the Pell series: -------------------------------------------------- The first 20 numbers of Pell series are: 0 1 2 5 12 29 70 169 408 985 2378 5741 13860 33461 80782 195025 470832 1136689 2744210 6625109
Flowchart:
C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C++ to check whether a given number is palindrome or not.
Next: Write a program in C++ to check if a number is Keith or not.
What is the difficulty level of this exercise?