How to print Lucas series in C++

Lets first learn what a Lucas series is and how to print Lucas series in C++.

We all know what a Fibonacci series is….1,1,2,3,5,8.13….and so on. For those who don’t know about it, it is a series that starts with 1 and 1 and continues to proceed in a fashion such that the next number is the sum of the previous two numbers.

Lucas series is of the same family but it starts with 2,1… instead of 1,1…The rest is just the same. 2,1,3,4,7,11…The next number is the sum of the previous two numbers.

The formula for both Fibonacci series and Lucas series is c=a+b where a and b equals to 1 in Fibonacci series and a=2 and b=1 in Lucas series.

Algorithm of lucas series:

  1.  Take a=2,b=1,c=0 at the beginning.
  2.  begin for(i=0 upto n-1)
  3.  check if(i=0) then
  4. print 2.
  5. end if.
  6. check if(i=1) then
  7. print 1.
  8. end if.
  9. if(i>1) then
  10. c=a+b,a=b,b=c and print c.
  11. end if
  12. end for

Example of the Lucas series:

Input:5
Output:2, 1, 3, 4, 7
Input:9
Output:2, 1, 3, 4, 7, 11, 18, 29, 47

 

The program displayed here will give the output to which we want to print the Lucas series.

C++ Program to print Lucas Numbers (Series)

#include <iostream> 
using namespace std;
void lucasnum(int n) 
{ 
  int a = 2, b = 1, c, i; 
  for (i = 0; i < n; i++) 
  { 
      if (i == 0) 
    cout<<2<<" ";
            else if (i==1) 
    cout<<1<<" ";
    else
    {
        c = a + b; 
        a = b; 
        b = c; 
            cout<<c<<" ";
    }
  } 
} 
int main() 
{ 
    int n ; 
    cout<<"Input the number of terms of Lucas numbers you want to print : ";
    cin>>n;
  	lucasnum(n);                 //calling Lucas function
  return 0; 
} 

The output of the above code:

How to print Lucas series in C++

You may also read:

Leave a Reply

Your email address will not be published. Required fields are marked *