Fibonacci Series Algorithm:
- Start
- Declare variables i, a,b , show
- Initialize the variables, a=0, b=1, and show =0
- Enter the number of terms of Fibonacci series to be printed
- Print First two terms of series
- Use loop for the following steps
-> show=a+b
-> a=b
-> b=show
-> increase value of i each time by 1
-> print the value of show - End
Fibonacci Series Flowchart:
PROG
#include <iostream>
using namespace std;
int main() {
int n, firstTerm = 1, secondTerm = 1, nextTerm;
cout << "Enter number of terms: ";
cin >> n;
cout << "Fibonacci Series: " << firstTerm << " + " << secondTerm << " + ";
for (int i = 1; i <= n-2; ++i) {
nextTerm = firstTerm + secondTerm;
cout << nextTerm << " + ";
firstTerm = secondTerm;
secondTerm = nextTerm;
}
return 0;
}
No comments:
Post a Comment