Question

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 n;
}
return fibonacciRecursive(n-1) + fibonacciRecursive(n-2);
}

int main() {
int n;
cout << "Enter a value for n: ";
cin >> n;
cout << n << "th fibonacci number is " << fibonacci(n) << endl;
cout << n << "th fibonacci number is " << fibonacciRecursive(n) << endl;
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

int fibonacci(int n) {
long 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;
// if b cross 1000000 then return
if(b>n)
return b;
}
return b;
}


int main() {
long int n=1000000;//just limit for fibonacci series

cout <<" fibonacci number Greator than 1000000 is " << fibonacci(n) << endl;
n=1000000000;
cout <<" fibonacci number Greator than 1000000000 is " << fibonacci(n) << endl;

return 0;
}

/*

fibonacci number Greator than 1000000 is 1346269
fibonacci number Greator than 1000000000 is 1134903170

*/

Add a comment
Know the answer?
Add Answer to:
Use the functions below to write a C++ program to find the smallest Fibonacci number greater...
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
  • 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; }

  • 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;...

  • Write following program using Switch statement. #include <iostream> using namespace std; int main() int number; cout...

    Write following program using Switch statement. #include <iostream> using namespace std; int main() int number; cout << "Enter an integer cin >> number; if (number > B) cout << You entered a positive integer: " << number << endl; else if (number (8) cout<<"You entered a negative integer: " << number << endl; cout << "You entered e." << endl; cout << "This line is always printed." return 0;

  • Write a Pseudocode for the below implementation of Greedy Algorithm to find the number of the...

    Write a Pseudocode for the below implementation of Greedy Algorithm to find the number of the least truck can be used to deliver items in C++. #include <iostream> using namespace std; void TruckFunction(int n , int w[], int limit) { int trucks = 1; int weight = 0; cout<<endl<<"Items carried in truck " <<trucks<<": "<<endl; for ( int i = 0 ; i < n ; i ++ ) { if((weight + w[i]) <= limit) { weight += w[i]; cout<<"Item...

  • 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 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 fibonaccivoid main(){  int n;  cout << "Input n " << endl;  cin >> n;  cout << "The Fibonacci Number corresp to"  << n << " is ";  fibonacci(n);}

  • Create a new file (in Dev C++) Modify program above to use a vector instead of...

    Create a new file (in Dev C++) Modify program above to use a vector instead of an array. Header comments must be present Prototypes must be present if functions are used Hello and goodbye messages must be shown Vector(s) must be used for implementation Use comments and good style practices ====================================================================================================== #include <iostream> using namespace std; int main() { cout<<"Welcome! This program will find the minimum and maximum numbers in an array."<<endl; cout<<"You will be asked to enter a number...

  • Question 19 4 pts Using the program below, please complete the program by adding 2 functions...

    Question 19 4 pts Using the program below, please complete the program by adding 2 functions as follow: 1. A function named getAverage that calculates and returns the Average. 2. A function named getMaximum that returns the maximum of the three numbers. Make sure to use the proper data types for all variables and functions. #include <iostream> using namespace std; //function getAverage //function getMaximum int main(int argc, char** argv) { int x, y, z; cout << "Enter three whole numbers:...

  • 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...

  • Write the missing statements for the following program. #include <iostream> using namespace std; int main(void) {...

    Write the missing statements for the following program. #include <iostream> using namespace std; int main(void) { int Num1; cout << "Enter 2 numbers: ";    cin >> Num2; if (Num1 < Num2) cout << "Smallest number is " << Num1; else cout << "Smallest number is " << Num2;    return 0; }

  • Bly language program that c l orresponds to the following Cr+ program.Include the memory addr Wri...

    bly language program that c l orresponds to the following Cr+ program.Include the memory addr Write a Pep/9 a include <iostream> using namespace std; int num; int main ( cout << "Enter a number:" cin >> num ; num = num * 4; cout << "num 4-<< num << endl; return 0 bly language program that c l orresponds to the following Cr+ program.Include the memory addr Write a Pep/9 a include using namespace std; int num; int main (...

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