[go: up one dir, main page]

0% found this document useful (0 votes)
21 views4 pages

Fibonocci Series

C++ Program

Uploaded by

SJV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views4 pages

Fibonocci Series

C++ Program

Uploaded by

SJV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Here's a simple program in Turbo C++ to generate the Fibonacci series up to a specified number of

terms. The Fibonacci series is a sequence where each number is the sum of the two preceding ones,
starƟng from 0 and 1.

```cpp

#include <iostream>

using namespace std;

void fibonacci(int n) {

int a = 0, b = 1, c;

// Print the first two terms

cout << a << " " << b;

for (int i = 2; i < n; i++) {

c = a + b;

cout << " " << c;

a = b;

b = c;

cout << endl;

int main() {

int n;

// Input the number of terms

cout << "Enter the number of terms: ";

cin >> n;

// Check if the number of terms is at least 2

if (n < 2) {
cout << "Number of terms should be at least 2." << endl;

return 1;

// Generate the Fibonacci series

fibonacci(n);

return 0;

```

### ExplanaƟon:

1. **FuncƟon DefiniƟon (`fibonacci`)**:

- Takes an integer `n` as input, represenƟng the number of terms in the Fibonacci series.

- IniƟalizes the first two terms of the series: `a = 0` and `b = 1`.

- Prints the first two terms.

- Uses a loop to generate the next terms by summing the two previous terms, updaƟng `a` and `b`
accordingly, and prinƟng each new term.

2. **Main FuncƟon**:

- Prompts the user to input the number of terms.

- Checks if the number of terms is at least 2 (since the Fibonacci series starts with 0 and 1).

- Calls the `fibonacci` funcƟon to generate and print the series.

This program will generate the Fibonacci series up to the specified number of terms entered by the
user. The series starts with 0 and 1, and each subsequent number is the sum of the two preceding
numbers.
#include <iostream.h>

#include <conio.h>

void fibonacci(int n) {

int a = 0, b = 1, c;

// Print the first two terms

cout << a << " " << b;

for (int i = 2; i < n; i++) {

c = a + b;

cout << " " << c;

a = b;

b = c;

cout << endl;

int main() {

int n;

// Input the number of terms

cout << "Enter the number of terms: ";

cin >> n;

// Check if the number of terms is at least 2

if (n < 2) {

cout << "Number of terms should be at least 2." << endl;

return 1;

}
// Generate the Fibonacci series

fibonacci(n);

getch(); // To keep the console window open

return 0;

You might also like