Fibonacci Sequence

0 Ethan Hann · September 22, 2015
This is my Fibonacci Sequence program that calculates the Fibonacci sequence up to a specified number of terms. Unfortunately, even using unsigned int's, the program can only calculate up to 47 terms accurately. If anyone knows a way around this, please reply. Anyway, here is the code (for some reason I can't put the whole thing in the {code} tags. Sorry about the poor layout :(

/*
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Copyright (c) Ethan Hann, 2015
*/
#include <iostream>
#include <stdlib.h>

using namespace std;

void evenFib(int number_of_terms);
void fib(int number_of_terms);
void oddFib(int number_of_terms);
void fibSum(int number_of_terms);

int main()
{
    int length = 0;
    char letter_choice = 'z';

    cout << "Fibonacci Sequence Counter" << endl;
    cout << "How many numbers do you want the sequence to be (from 1 to 47): ";
    cin >> length;
    cout << "Now, do you want to sum all even numbers, all odd numbers, all numbers, or none?" << endl;
    cout << "(e)ven, (o)dd, (a)ll numbers, (n)one: ";
    cin >> letter_choice;
    cout << "One second." << endl;
    _sleep(500);
    cout << "One second.." << endl;
    _sleep(500);
    cout << "One second..." << endl;
    _sleep(500);

    int x = 1;
    while (x > 0)
    {
        if (letter_choice == 'e')
        {
            evenFib(length);
            x -= 1;
        } else if (letter_choice == 'o')
        {
            oddFib(length);
            x -= 1;
        } else if (letter_choice == 'n')
        {
            fib(length);
            x -= 1;
        } else if (letter_choice == 'a')
        {
            fibSum(length);
            x -= 1;
        } else
        {
            cout << "Incorrect option for letter." << endl;
            cout << "Now, do you want to sum all even numbers, all odd numbers, all numbers, or none?" << endl;
            cout << "(e)ven, (o)dd, (a)ll numbers, (n)one: ";
            cin >> letter_choice;
        }
        cout << "How many numbers do you want the sequence to be (from 1 to 47): ";
        cin >> length;
        cout << "Now, do you want to sum all even numbers, all odd numbers, all numbers, or none?" << endl;
        cout << "(e)ven, (o)dd, (a)ll numbers, (n)one, (q)uit: ";
        cin >> letter_choice;
        if (letter_choice == 'q')
        {
            x -= 1;
        }
        else
        {
            x += 1;
        }
    }
}

void evenFib(int number_of_terms)
{
    int i = 0;
    int even_sum = 0;

unsigned int a = 1;
unsigned int b = 0;

unsigned long fibonacci_number = 0;

while(i < number_of_terms)
{
b = fibonacci_number;
fibonacci_number = a + b;
if (fibonacci_number % 2 == 0)
        {
            even_sum += fibonacci_number;
        }
cout << "Fibonacci number " << i+1 << ":" << " " << fibonacci_number << "\n";

a = b;
i++;
}
cout << endl;
cout << "The sum of all even Fibonacci Numbers within the specified parameters is: " << even_sum << endl;
}

void oddFib(int number_of_terms)
{
    int i = 0;
    int odd_sum = 0;

unsigned int a = 1;
unsigned int b = 0;

unsigned long fibonacci_number = 0;

while(i < number_of_terms)
{
b = fibonacci_number;
fibonacci_number = a + b;
if (fibonacci_number % 2 != 0)
        {
            odd_sum += fibonacci_number;
        }
cout << "Fibonacci number " << i+1 << ":" << " " << fibonacci_number << "\n";

a = b;
i++;
}
cout << endl;
cout << "The sum of all odd Fibonacci Numbers within the specified parameters is: " << odd_sum << endl;
}

void fib(int number_of_terms)
{
int i = 0;

unsigned int a = 1;
unsigned int b = 0;

unsigned long fibonacci_number = 0;

while(i < number_of_terms)
{
b = fibonacci_number;
fibonacci_number = a + b;

cout << "Fibonacci number " << i+1 << ":" << " " << fibonacci_number << "\n";

a = b;
i++;
}
cout << endl;
}

void fibSum(int number_of_terms)
{
    int i = 0;
    int sum = 0;

unsigned int a = 1;
unsigned int b = 0;

unsigned long fibonacci_number = 0;

while(i < number_of_terms)
{
b = fibonacci_number;
fibonacci_number = a + b;
sum += fibonacci_number;
cout << "Fibonacci number " << i+1 << ":" << " " << fibonacci_number << "\n";

a = b;
i++;
}
cout << endl;
cout << "The sum of all Fibonacci Numbers within the specified parameters is: " << sum << endl;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · September 23, 2015
I don't get half of the stuff involving evens and odds. 

This is just a thought, but less is more.

Have one function for calculating the sequence. Make an additional input for the starting number and another term for the incrementation amount or would that not work?


However, I don't understand why you aren't using unsigned longs if you really want big numbers.
0 ishwiin doe · September 24, 2015
/images/forum/upload/2015-09-23/d6d0ff1ec469904a2e8ee1041628178b.PNG
best i could do
  • 1

C++

107,247 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator