C++ Exercises: Print the first 20 numbers of the Pell series
C++ Numbers: Exercise-39 with Solution
Write a C++ program 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: Find Strong Numbers within a range of numbers.
What is the difficulty level of this exercise?
C++ Programming: Tips of the Day
Why does the C++ map type argument require an empty constructor when using []?
This issue comes with operator[]. Quote from SGI documentation:
data_type&operator[](constkey_type& k) - Returns a reference to the object that is associated with a particular key. If the map does not already contain such an object, operator[] inserts the default object data_type().
If you don't have default constructor you can use insert/find functions. Following example works fine:
myMap.insert(std::map<int, MyClass>::value_type ( 1, MyClass(1) ) ); myMap.find( 1 )->second;
Ref: https://bit.ly/3PQBRJi
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook