the fibonacci sequence is a set of numbers that starts with a zero, followed by a one and proceeds based on the rule that each number( called a Fibonacci number) is equal to the sum of the first two numbers. the Fibonacci sequence is denoted by F(n), where n is the first term in the sequence. the following equation is obtained when n= 0, where the first two terms are 0 and 1 , and each subsequent number is the sum of the previous two numbers
#include <iostream>
using namespace std;
int main() {
int numberTerms;
int term1 = 0;
int term2 = 1;
cout << "Enter the number of terms: ";
cin >> numberTerms;
if (numberTerms>=1){
cout << "The Fibonacci sequence:\n";
cout<<term1 << " ";
for (int i = 1; i < numberTerms; i++){
cout<<term2<< " ";
int next = term1 + term2;
term1 = term2;
term2 = next;
}
}
cout<<"\n\n";
system("pause");
return 0;
}
Comments
Leave a comment