# Generate Fibonacci Numbers in C++

This article will demonstrate multiple methods of how to generate Fibonacci numbers in C++.

## C++ Use Iterative Method to Print the First `n` Items in Fibonacci Sequence

Fibonacci numbers are commonly known in math as a sequence of numbers where each item is the sum of the two preceding ones, starting from `0` and `1`. In this example code, we take the user’s input as integer `n` and generate the first `n` numbers in the Fibonacci sequence. The solution is plain iterative, where we initialize the starting values and iterate `n-3` times to sum the previous values. Each iteration, the sum is printed to `cout` stream, and stored values are shifted by one number window. Note that the `if` condition is implemented, which checks for the input integer - `0` and prints the corresponding value. The first `cout` statement handles the case when `n` equals `2`, as the `for` loop condition evaluates to false and does not proceed.

``````#include <iostream>

using std::cout; using std::cin;
using std::endl; using std::string;

void generateFibonacci(unsigned long long n)
{
if (n == 1) {
cout << 0 << endl;
return;
}

unsigned long long a = 0;
unsigned long long b = 1;
unsigned long long c;

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

for (unsigned long long i = 3; i <= n; i++)
{
c = a + b;
cout << " " << c;
a = b;
b = c;
}
}

int main()
{
unsigned long long num;

cout << "Enter the number of items to generate Fibonacci series: ";
cin >> num;

generateFibonacci(num);

return EXIT_SUCCESS;
}
``````

Output:

``````Enter the number of items to generate Fibonacci series: 10
0 1 1 2 3 5 8 13 21 34
``````

## C++ Use Iterative Method to Print the `n`-Th Item in Fibonacci Sequence

Alternatively, we can implement a function that returns the `n`-th number in the Fibonacci sequence. Note that we store user input in `unsigned long long` because we want to be able to output the largest numbers that can be stored by built-in C++ types. The function starts with the `if-else` statement to check for the first three cases and return hard-coded values. On the other hand, if `n` is larger than `3`, we proceed to loop block and iterate until we get the `n`-th number in the sequence.

``````#include <iostream>

using std::cout; using std::cin;
using std::endl; using std::string;

unsigned long long generateFibonacci(unsigned long long n)
{
if (n == 1) {
return 0;
} else if (n == 2 || n == 3) {
return 1;
}

unsigned long long a = 1;
unsigned long long b = 1;
unsigned long long c;

for (unsigned long long i = 3; i < n; i++)
{
c = a + b;
a = b;
b = c;
}

return c;
}

int main()
{
unsigned long long num;

cout << "Enter the n-th number in Fibonacci series: ";
cin >> num;

cout << generateFibonacci(num);
cout << endl;

return EXIT_SUCCESS;
}
``````

Output:

``````Enter the n-th number in Fibonacci series: 10
34
``````
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

## Related Article - C++ Math

• Raise Number to Power in C++