The Program in C++ to Display the first n terms of Fibonacci series given below:
#include <iostream>
using namespace std;
int main() {
int n;
int first = 0, second = 1, next;
cout << "Hello Codeauri Family, enter the number of terms in the series here:\n";
cin >>n;
cout << "Well, the first " << n << " terms of the Fibonacci series are: ";
for (int i = 0; i < n; i++) {
if (i <= 1) {
next = i;
} else {
next = first + second;
first = second;
second = next;
}
cout << next << " ";
}
cout << endl;
return 0;
}
Output:
Hello Codeauri Family, enter the number of terms in the series here:
9
Well, the first 9 terms of the Fibonacci series are: 0 1 1 2 3 5 8 13 21
Pro-Tips💡
This program prompts the user to enter an integer value for n
, and then uses a for loop to generate the first n
terms of the Fibonacci series.
The terms are then printed out.
The Fibonacci series is a series of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1.
It uses standard input/output library <iostream>
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.