ashpatel

Fibonacci numbers using queue

Nov 12th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.43 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. queue<unsigned long long> q;
  5.  
  6. void fibo_using_queue(int n){
  7.     q.push(0);
  8.     q.push(1);
  9.     for (int i = 0; i < n; i++){
  10.         unsigned long long a = q.front();q.pop();
  11.         unsigned long long b = q.front();q.pop();
  12.         q.push(b);
  13.         q.push(a + b);
  14.         cout<<a<<endl;
  15.     }
  16. }
  17.  
  18. int main(){
  19.     fibo_using_queue(86);//print first 86 Fibonacci numbers
  20. }
Add Comment
Please, Sign In to add comment