-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrint first n Fibonacci Numbers.cpp
58 lines (50 loc) · 1.11 KB
/
Print first n Fibonacci Numbers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//{ Driver Code Starts
//Initial function template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution
{
public:
//Function to return list containing first n fibonacci numbers.
vector<long long> printFibb(int n)
{
//code here
if(n==1 ) return {n};
if(n==2) return {1,1};
vector<long long>ans;
long long a=1;
long long b=1;
ans.push_back(a);
ans.push_back(b);
for(int i=2;i<n;i++){
long long c=a+b;
ans.push_back(c);
a=b;
b=c;
}
return ans;
}
};
//{ Driver Code Starts.
int main()
{
//taking total testcases
int t;
cin>>t;
while(t--)
{
//taking number of elements
int n;
cin>>n;
Solution obj;
//calling function printFibb()
vector<long long> ans = obj.printFibb(n);
//printing the elements of vector
for(long long i:ans)cout<<i<<' ';
cout<<endl;
}
return 0;
}
// } Driver Code Ends