This is what I have to do.

Write a program that has a Fibonacci number calculating function. The function will take an integer as a parameter which is the Fibonacci number to calculate, and return the Fibonacci number. Then use this function to calculate the sum of every Fibonacci number that is divisible by 3 or 5, (hint - use modulus division). Do this for all Fibonacci numbers less than 4,000,000. Make sure that you document your program.

I have most of the code done and the only part I can't figure out is how to get the function that gets the sun of every Fibonacci number that is divisible by 3 or 5. I've tried a for loop but the numbers just kept scrolling on the output. This thing has been plaguing me for the past day and a half. Any help would be greatly appreciated.

using namespace std;
void getInput(int& numbers);//Function declaration to get information
void calculations(int& numbers, int& fib3);//Function declaration to perform calculations
void getNumbersthree(int& fib3);
void printOutput(int& numbers, int& fib3);//Function declaration to print out the requested information.
char getResponse();//Function declaration to repeat program.
const int MILLIONS = 4000000;
const int MAXFIBNUM = 33;
const int ZERO = 0;

int main()
{
    int numbers = 2, fib3;//Variables that are used in main.
    char response;//Variables that are used in main.
    do
    {         
         getInput(numbers);//Used to call the getInput function.
         calculations(numbers, fib3);//Used to call the calculations function.
         getNumbersthree(fib3);//Used to call the getNumbersthree function.
         printOutput(numbers, fib3);//Used to call the printOutput function.
         response = getResponse();//Used to call the getResponse function.
    }while(response == 'y');
return 0;
}
void getInput(int& numbers)//Function that will ask the user to input the data.
{
    cout << "\nPlease enter a number between 0 and 33.  ";//Asks for a number between 0 and 33. Since F(33) is less than 4000000 a larger number would not apply.
    cin >> numbers;
    
	if ( numbers > MAXFIBNUM)//If - Else loop to determine if the number entered is greater than 33. If great 
    {
         cout << "\nThe number you have entered will produce a number greater than 4,000,000. "
              << "This program is designed for Fibonacci numbers that are less than 4,000,000." << endl;
         getInput(numbers);//Allows the program to rerun from the beginning if the number is greater than 33.
    } 
    else
    {
         cout << "\nThank you for your input. Your numbers will appear below." << endl;
    }
     
}
void calculations(int& numbers, int& fib3)//This function calculates the Fibonacci sequence of numbers.
{
     
     int counter = 0, fib1 = 0, fib2 = 0;//Local variables.
     do
     {
          counter++;//Allows the counter to increase.
          fib3 = (fib1 + fib2 == 0) ? 1 : fib1 + fib2;//This is the main algorithm allowing the math portion to take place.
          //cout << "\t" << fib3;//Output statement to check that numbers are correct.
          fib1 = fib2;//fib1 equals fib2
          fib2 = fib3;//fib2 equals fib3
     } while (counter < numbers);
     
     return;     
}
void getNumbersthree(int& fib3)
{
     
     
     
}

void printOutput(int& numbers, int& fib3)//This function prints out the output from the program.
{
    
    if ( numbers == ZERO)//This portion of the if-else if-else loop allows the program to print out a zero if the user inputs a zero as their first Fibonacci number
    {
    
         cout << endl; 
         cout << "\nThe number you entered is: " << numbers << ". The Fibonacci number that corresponds is: 0." << endl; 
    }
    else if ( numbers > MAXFIBNUM)//This portion of the loop prints out nothing if the number entered is greater than 33.
    {
             
    }
    else//This portion of the loop prints out the information requested as long as it is not a 0 or above 33.
    {
    
         cout << endl; 
         cout << "\nThe number you entered is: " << numbers << ". The Fibonacci number that corresponds is: " << fib3 << "." << endl; 
    }       
}
    char getResponse()//This is the function that will ask for input from the user.
    {
        using namespace std;//Used per function no longer used globally.
    	char response;//Local variable.
    	do
    	{
            cout << "\nDo you want to continue? (y for yes, n for no): ";//Prompt asking if continuation is wanted.
            cin  >> response;

            if (response != 'y' && response != 'n' )//The if-loop to make sure the user inputs a y or n to get appropriate input.

                cout << "\nInvalid input, enter y or n" << endl;//Output asking user to make sure to use correct input.

        }while(response != 'y' && response != 'n');
        return response;
    }

Recommended Answers

All 2 Replies

This is what I have to do.

Write a program that has a Fibonacci number calculating function. The function will take an integer as a parameter which is the Fibonacci number to calculate, and return the Fibonacci number. Then use this function to calculate the sum of every Fibonacci number that is divisible by 3 or 5, (hint - use modulus division). Do this for all Fibonacci numbers less than 4,000,000. Make sure that you document your program.

I have most of the code done and the only part I can't figure out is how to get the function that gets the sun of every Fibonacci number that is divisible by 3 or 5. I've tried a for loop but the numbers just kept scrolling on the output. This thing has been plaguing me for the past day and a half. Any help would be greatly appreciated.

using namespace std;
void getInput(int& numbers);//Function declaration to get information
void calculations(int& numbers, int& fib3);//Function declaration to perform calculations
void getNumbersthree(int& fib3);
void printOutput(int& numbers, int& fib3);//Function declaration to print out the requested information.
char getResponse();//Function declaration to repeat program.
const int MILLIONS = 4000000;
const int MAXFIBNUM = 33;
const int ZERO = 0;

int main()
{
    int numbers = 2, fib3;//Variables that are used in main.
    char response;//Variables that are used in main.
    do
    {         
         getInput(numbers);//Used to call the getInput function.
         calculations(numbers, fib3);//Used to call the calculations function.
         getNumbersthree(fib3);//Used to call the getNumbersthree function.
         printOutput(numbers, fib3);//Used to call the printOutput function.
         response = getResponse();//Used to call the getResponse function.
    }while(response == 'y');
return 0;
}
void getInput(int& numbers)//Function that will ask the user to input the data.
{
    cout << "\nPlease enter a number between 0 and 33.  ";//Asks for a number between 0 and 33. Since F(33) is less than 4000000 a larger number would not apply.
    cin >> numbers;
    
	if ( numbers > MAXFIBNUM)//If - Else loop to determine if the number entered is greater than 33. If great 
    {
         cout << "\nThe number you have entered will produce a number greater than 4,000,000. "
              << "This program is designed for Fibonacci numbers that are less than 4,000,000." << endl;
         getInput(numbers);//Allows the program to rerun from the beginning if the number is greater than 33.
    } 
    else
    {
         cout << "\nThank you for your input. Your numbers will appear below." << endl;
    }
     
}
void calculations(int& numbers, int& fib3)//This function calculates the Fibonacci sequence of numbers.
{
     
     int counter = 0, fib1 = 0, fib2 = 0;//Local variables.
     do
     {
          counter++;//Allows the counter to increase.
          fib3 = (fib1 + fib2 == 0) ? 1 : fib1 + fib2;//This is the main algorithm allowing the math portion to take place.
          //cout << "\t" << fib3;//Output statement to check that numbers are correct.
          fib1 = fib2;//fib1 equals fib2
          fib2 = fib3;//fib2 equals fib3
     } while (counter < numbers);
     
     return;     
}
void getNumbersthree(int& fib3)
{
     
     
     
}

void printOutput(int& numbers, int& fib3)//This function prints out the output from the program.
{
    
    if ( numbers == ZERO)//This portion of the if-else if-else loop allows the program to print out a zero if the user inputs a zero as their first Fibonacci number
    {
    
         cout << endl; 
         cout << "\nThe number you entered is: " << numbers << ". The Fibonacci number that corresponds is: 0." << endl; 
    }
    else if ( numbers > MAXFIBNUM)//This portion of the loop prints out nothing if the number entered is greater than 33.
    {
             
    }
    else//This portion of the loop prints out the information requested as long as it is not a 0 or above 33.
    {
    
         cout << endl; 
         cout << "\nThe number you entered is: " << numbers << ". The Fibonacci number that corresponds is: " << fib3 << "." << endl; 
    }       
}
    char getResponse()//This is the function that will ask for input from the user.
    {
        using namespace std;//Used per function no longer used globally.
    	char response;//Local variable.
    	do
    	{
            cout << "\nDo you want to continue? (y for yes, n for no): ";//Prompt asking if continuation is wanted.
            cin  >> response;

            if (response != 'y' && response != 'n' )//The if-loop to make sure the user inputs a y or n to get appropriate input.

                cout << "\nInvalid input, enter y or n" << endl;//Output asking user to make sure to use correct input.

        }while(response != 'y' && response != 'n');
        return response;
    }

I would just do it in your calculations function.

int calculations(int& numbers, int& fib3)//This function calculates the Fibonacci sequence of numbers.
{
     
     int counter = 0, fib1 = 0, fib2 = 0;//Local variables.
     int sum = 0;
     do
     {
		 
          counter++;//Allows the counter to increase.
          fib3 = (fib1 + fib2 == 0) ? 1 : fib1 + fib2;//This is the main algorithm allowing the math portion to take pla
          if (fib3 % 3 == 0 || fib3 % 5 == 0) sum += fib3;
		  //cout << "\t" << fib3;//Output statement to check that numbers are correct.
          fib1 = fib2;//fib1 equals fib2
          fib2 = fib3;//fib2 equals fib3
     } while (counter < numbers);
     
     return sum;
}

Thanks for your help. I think I was starring at the problem to long for a result to come to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.