write a c++ program to print the fibanacci series for n terms and save it as fibo.cpp write python program to execute this c++ program
Answers
Answered by
0
Language:
C++
Program:
#include <iostream>
using namespace std;
int main() { int n, a = 0, b = 1, c = 0;
cout << "Enter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: "; cout<<"\n";
for (int i = 1; i <= n; i++) {
if(i == 1) { cout << a; cout<<"\n"; continue; }
if(i == 2) { cout << b; cout<<"\n"; continue; }
c = a + b;
a = b;
a = c;
cout << c;
cout<<"\n"; }
return 0; }
Output:
Consider the attachment.
Explanation:
- Takes n as input.
- Runs loop till n.
- i++ increases terms every time by 1.
- If statement lets the first 2 element print i.e 0 and 1.
- c is the sum of a and b printed when the no of times the loop ran exceeds 2.
Attachments:
Similar questions