C++ Program to Display Fibonacci Series
The Fibonacci series is a sequence of numbers such that each number is the sum of the two preceding ones, usually starting with 0 and 1. The sequence starts (0, 1, 1, 2, 3, 5, 8, 13, 21, ……).
Here’s how you can write a Turbo C++ program to display the Fibonacci series up to (n) terms:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr(); // Clear the screen (specific to Turbo C++)
int n, first = 0, second = 1, next;
cout << "Enter the number of terms for Fibonacci series: ";
cin >> n;
cout << "First " << n << " terms of Fibonacci series are: ";
for (int i = 1; i <= n; ++i) {
if (i == 1) {
cout << first << ", ";
continue;
}
if (i == 2) {
cout << second << ", ";
continue;
}
next = first + second;
first = second;
second = next;
cout << next;
// Print comma for all terms except the last one
if (i < n) {
cout << ", ";
}
}
cout << endl;
getch(); // Wait for user input (specific to Turbo C++)
}
To compile and run this program in Turbo C++:
- Open Turbo C++.
- Create a new file and copy-paste the above code into it.
- Save the file.
- Compile the code (usually by pressing Alt + F9).
- Run the program (usually by pressing Ctrl + F9).
The program will prompt you to enter the number of terms you’d like to see in the Fibonacci series. It will then display the first (n) terms of the Fibonacci series.
