1#include <iostream>
2using namespace std;
3int main()
4{
5 int num = 1, pre_num = 0, sum, n;
6 do
7 {
8 cout << "Enter number of terms 'n'of Fibonacci series: ";
9 cin >> n;
10 cout << "Fibonacci series: ";
11 for (int i = 0; i < n; i++)
12 {
13 sum = num + pre_num;
14 pre_num = num;
15 num = sum;
16 cout << num << " ";
17 }
18 return 0;
19}
1#include <iostream>
2using namespace std;
3
4int main(){
5 int a{0},b{1},count{},c{};
6 cin >> count;
7 for (int i = 0; i < count; ++i)
8 {
9 cout << a << endl;
10 c = b;
11 b = a + b;
12 a = c;
13 }
14}
1#include <iostream>
2using namespace std;
3
4int main ()
5{
6 int num1, num2, num3, input;
7 num1 = 0;
8 num2 = 1;
9 cout << "How many nums in sequence do you want to see: ";
10 cin >> input;
11 cout << num1 << " " << num2 << " ";
12
13 for(int i = 2; i < input; i++) // runs through every element but first two
14 {
15 num3 = num1 + num2;
16 cout << num3 << " ";
17 num1 = num2;
18 num2 = num3;
19 }
20
21 return 0;
22}
1#include<iostream>
2using namespace std;
3int main()
4{
5 int num,num1=0,num2=1,num3,i=0;
6 cout<<"enter the number num"<<endl;
7 cin>>num;
8 for(i=1;i<=num;i++)
9 {
10 num3=num1+num2;
11 num1=num2;
12 num2=num3;
13 cout<<num3<<endl;
14 }
15
16}
17
1//dp ;Bottom up approach
2#include <iostream>
3
4using namespace std;
5int fib(int n)
6{
7 int fib[n+1];
8 fib[0]=0;
9 fib[1]=1;
10 for(int i=2;i<=n;i++)
11 {
12 fib[i]=fib[i-1]+fib[i-2];
13 }
14 return fib[n];
15}
16
17int main()
18{
19 int n;
20 cout<<"enter the value whose fibonaaci value you want to claculate:"<<endl;
21 cin>>n;
22 if(n<=1)
23 {
24 cout<<"fib is: "<<n<<endl;
25 }
26 else
27 {
28 cout<<"fib is: "<<fib(n)<<endl;
29 }
30 return 0;
31}
32
1#include <iostream>
2using namespace std;
3
4int main()
5{
6 int n, t1 = 0, t2 = 1, nextTerm = 0;
7
8 cout << "Enter the number of terms: ";
9 cin >> n;
10
11 cout << "Fibonacci Series: ";
12
13 for (int i = 1; i <= n; ++i)
14 {
15 // Prints the first two terms.
16 if(i == 1)
17 {
18 cout << " " << t1;
19 continue;
20 }
21 if(i == 2)
22 {
23 cout << t2 << " ";
24 continue;
25 }
26 nextTerm = t1 + t2;
27 t1 = t2;
28 t2 = nextTerm;
29
30 cout << nextTerm << " ";
31 }
32 return 0;
33}
34