Header Ads Widget

C++ program to construct a Fibonacci series up to n terms

 C++ program to construct a Fibonacci series up to n terms 

Given a number n , write a program to construct a Fibonacci series up to n terms . So first we have to know what is Fibonacci series . 

 what is fibonacci series:- 

                     In the Fibonacci series every elements are the some of two elements before it.

Fibonacci Sequence = 0, 1, 1, 2, 3, 5, 8, 13, 21, ….


Sample input

6

Sample output

0 1 1 2 3 5

 C++ program to construct a Fibonacci series up to n terms 

The objective of the code is to construct a Fibonacci series up to given number of terms .

Code(C++):-

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,pp=0,p=1,nx,i; //pp=previous previous term
// p=previous term,nx=next term
cout<<"Enter the number of term in Fibonacci series\n";
cin>>n;
cout<<pp<<" "<<p<<" ";
for(i=2;i<n;i++)
{
nx=pp+p;
cout<<nx<<" ";
pp=p;
p=nx;
}
return 0;
}

Output:

Enter the number of term in Fibonacci series
6
0 1 1 2 3 5

Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-