C++ function to calculate Fibonacci number sequence

In this artcile you will learn how calculate Fibonacci sequence with using C++ function. The Fibonacci sequence starts with 0 and 1 where the the following number is always a sum of the two preceding numbers. For example, 0,1,1,2,3,5,8 and so on.

In this tutorial you will learn:

  • How to compile Fibonacci C++ program
  • How to run Fibonacci C++ program

Fibonacci number sequence with C++

Fibonacci number sequence with C++

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software g++ compiler
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to calculate Fibonacci number sequence step by step instructions

  1. Install g++ compiler. The procedure is simple as follows:UBUNTU/DEBIAN
    # apt install g++
    

    RHEL/CENTOS

    # dnf groupinstall "Development Tools"
    

    Once ready, confirm the g++ compiler availability on your system:

    $ g++ --version
    g++ (Debian 8.3.0-6) 8.3.0
    


  2. Use your favourite text editor and save the below C++ code into a file called FibonacciNumber.cpp:
    #include <iostream>
    #include <cstdlib>
    
    void HowMany(int *numbers);
    void CalculateFibonacci(int *numbers);
    
    int main() {
    
    int numbers = 0;
    
    HowMany(&numbers);
    CalculateFibonacci(&numbers);
    
    return 0;
    }
    
    
    void HowMany(int *numbers)
    
    {
       // use pointers as parameter, not references
       std::cout << "How many Fibonacci numbers would you like to calculate? ";
       std::cin >> *numbers;
       if ((!std::cin.good()) || ( *numbers <= 1 ||  ( *numbers >= 48 )))
    
       {
          printf("Invalid number entered ! Enter number greater than 0 or less than < 48  \n");
          exit(1);
       }
    
    }
    
    void CalculateFibonacci(int *numbers) 
    {
    	int i = 0;
    
    	unsigned long a = 1;
    	unsigned long b = 0;
    
    	unsigned long fibonacci_number = 0;
    
    	while(i < *numbers) {
    		b= fibonacci_number;
    
    		fibonacci_number = a + b;
    		std::cout << "Fibonacci number: " << i+1 << ". " << fibonacci_number << "\n";
    
    		a = b;
    		i++;
    	}
    	std::cout << std::endl;
    }
  3. Compile the above C++ program into a FibonacciNumber executable binary. To do so run the following g++ command from the directory where your FibonacciNumber.cpp source code is located:
    $ g++ FibonacciNumber.cpp -o FibonacciNumber
    

    The above should compile without any output and errors. As a result, you now have the FibonacciNumber available in your directory:

    $ ls FibonacciNumber
    FibonacciNumber
    


  4. Calculate Fibonacci number sequence using the FibonacciNumber executable binary compiled in the previous step:
    $ ./FibonacciNumber
    How many Fibonacci numbers would you like to calculate? 10
    Fibonacci number: 1. 1
    Fibonacci number: 2. 1
    Fibonacci number: 3. 2
    Fibonacci number: 4. 3
    Fibonacci number: 5. 5
    Fibonacci number: 6. 8
    Fibonacci number: 7. 13
    Fibonacci number: 8. 21
    Fibonacci number: 9. 34
    Fibonacci number: 10. 55
    


Comments and Discussions
Linux Forum