Please help me with the correct code in c Problem 1 Fibonacc

Please help me with the correct code in c++!!

Problem 1: Fibonacci sequence

Write a program in C++ to accomplish the following:
a) Prompt user for an integer number n. Find and display the n-th Fibonacci number using a recursive function.

b) Do the same as (a) using a non-recursive function.

Sample input: 10
Sample output:
a) using recursive function: 55
b) using non-recursive function: 55

Solution

Fibonacci.cpp

#include <iostream>

using namespace std;
int recFibonacci(int n);
int NonecFibonacci(int n);
int main()
{
int n;
cout << \"Enter an integer number n: \";
cin >> n;
int result1 = recFibonacci(n);
cout<<\"using recursive function: \"<<result1<<endl;
int result2 = NonecFibonacci(n);
cout<<\"using non recursive function: \"<<result2<<endl;
return 0;
}

int recFibonacci(int n)
   {
       if (n <= 1)
       return n;
       return recFibonacci(n-1) + recFibonacci(n-2);
       }
int NonecFibonacci(int n)
   {
       int c=0;
   int a = 0;
   int b = 1;
   for (int i = 2; i <= n; i++)
   {
   c = a + b;
   a = b;
   b = c;
   }
   return c;
       }

Output:

sh-4.3$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                            

sh-4.3$ main                                                                                                                                                                                                                    

Enter an integer number n: 10                                                                                                                                                                                                   

using recursive function: 55                                                                                                                                                                                                    

using non recursive function: 55

Please help me with the correct code in c++!! Problem 1: Fibonacci sequence Write a program in C++ to accomplish the following: a) Prompt user for an integer nu
Please help me with the correct code in c++!! Problem 1: Fibonacci sequence Write a program in C++ to accomplish the following: a) Prompt user for an integer nu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site