Question

Please help with this Fibonacci Code (C++)

#include <iostream>


void fibonacci (int n)

// compute the Fibonacci number F sub n

{

   if (n <= 1)  cout << n << endl; // F sub 0 = 0 and F sub 1 = 1

   else { // compute F sub n

      int fnint fnm2 = 0int fnm1 = 1;

      for (int i = 2i<=ni++)

      {

     fn = fnm1 + fnm2;

     fnm2 = fnm1;

     fnm1 = fn;

      } // end of for

      cout << fn << endl;

   } // end of else

} // end of fibonacci



void main()

{

  int n;

  cout << "Input n " << endl;

  cin >> n;

  cout << "The Fibonacci Number corresp to"  << n << " is ";

  fibonacci(n);

}


0 0

> Can you kindly tell me what to do?

Zahidul Hossain Wed, Sep 29, 2021 7:44 AM

Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

void fibonacci (int n)

// compute the Fibonacci number F sub n

{

   if (n <= 1)  cout << n << endl; // F sub 0 = 0 and F sub 1 = 1

   else { // compute F sub n

      int fn; int fnm2 = 0; int fnm1 = 1;

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

      {

     fn = fnm1 + fnm2;

     fnm2 = fnm1;

     fnm1 = fn;

      } // end of for

      cout << fn << endl;

   } // end of else

} // end of fibonacci




int main()

{

  int n;

  cout << "Input n " << endl;

  cin >> n;

  cout << "The Fibonacci Number corresp to"  << n << " is ";

  fibonacci(n);

}


answered by: Zahidul Hossain

> main() function cannot be void. it should be int.

Zahidul Hossain Wed, Sep 29, 2021 8:24 AM

Add a comment
Know the answer?
Add Answer to:
Please help with this Fibonacci Code (C++)
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0,...

    How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...

  • Use the functions below to write a C++ program to find the smallest Fibonacci number greater...

    Use the functions below to write a C++ program to find the smallest Fibonacci number greater than 1,000,000 and greater than 1,000,000,000. #include <iostream> using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return...

  • C++ Fibonacci Complete ComputeFibonacci() to return FN, where F0 is 0, F1 is 1, F2 is...

    C++ Fibonacci Complete ComputeFibonacci() to return FN, where F0 is 0, F1 is 1, F2 is 1, F3 is 2, F4 is 3, and continuing: FN is FN-1 + FN-2. Hint: Base cases are N == 0 and N == 1. #include <iostream> using namespace std; int ComputeFibonacci(int N) { cout << "FIXME: Complete this function." << endl; cout << "Currently just returns 0." << endl; return 0; } int main() { int N = 4; // F_N, starts at...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code...

    #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code here } void fill(int arr[], int count){ for(int i = 0; i < count; i++){ cout << "Enter number: "; cin >> arr[i]; } } void display(int arr[], int count){ for(int i = 0; i < count; i++){ cout << arr[i] << endl; } } int main() { cout << "How many items: "; int count; cin >> count; int * arr = new...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • Add comments on this Fibonacci C++ program in detail. #include <iostream> using namespace std; int fib(int...

    Add comments on this Fibonacci C++ program in detail. #include <iostream> using namespace std; int fib(int n){ int a = 0,b = 1,c; if(n == 0 || n == 1){ return n; } while(n>2){ c = b + a; a = b; b = c; n--; } return c; } int main(){ int n; cout<<"Enter n: "; cin>>n; int result = fib(n); cout<<"Fib("<<n<<") = "<<result<<endl; return 0; }

  • im writing a c++ code and getting stuck on two parts. The first part, when I...

    im writing a c++ code and getting stuck on two parts. The first part, when I go to write the customer order out to the file, it writes everything but the price out right. I'll get a weird number like 5.95828e-039 or nan. When I printout to the console it works fine so not sure what's wrong. Second After I write the order out to the file, I then have to go in and read the file and calculate the...

  • Please help asap Q6 6" (15%) Fill up the missing bodies of factorialO and fibonacciO of...

    Please help asap Q6 6" (15%) Fill up the missing bodies of factorialO and fibonacciO of the following codes: #include <iostream> using namespace std; unsigned long factorial(unsigned long); unsigned long fibonacci(unsigned long); / / function prototype int main0 unsigned long size_factorial, size fibonacci; cout << "input an unsigned integer for the upper bound of factorialO" < endl; cin >> size factorial; cout << "input an unsigned integer for the upper bound of fibonacci)" <endl; cin >>size_fibonacci; cout << "the result...

  • Help with C++ reverse program with leading zeros. I need to put the line from the...

    Help with C++ reverse program with leading zeros. I need to put the line from the function that display the zeros in main not in the function. So how can I move the display with leading zeros in main. Thanks. Here is my code. #include <iostream> #include <cstdlib> #include <iostream> using std::cout; using std::cin; using std::endl; //function templates int reverseNum(int); int main() { //variables char buf[100]; int num; while (true) { //prompt user for input cout << "Enter the number...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT