Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using C# The Fibonacci sequence is a famous but of mathematics, and it happens t

ID: 3863006 • Letter: U

Question

using C# The Fibonacci sequence is a famous but of mathematics, and it happens to have a recursice definition. The first two values in the sequence are 0 and 1(essentially 2 base cases). Each subsequent value is the sum of the pervious two values, so the whole sequence is : 0,1,1,2,3,5,8,13,21 and so on. Write a recursive method, Fibonacci(n), that returns the nth Fibonacci number, with n=0 representing the start of the sequence. Write a non-recursive solution to Fibonacci using the same sequence and value. The first part is solved, needing help with the non recursive method.

Explanation / Answer

using System.IO;
using System;

class Program
{
static void Main()
{
Console.WriteLine("Enter the nth fibonacci value: ");
int n=Convert.ToInt32(Console.ReadLine());
int nthValue = NonFibonacci(n);
Console.WriteLine(n+"th fibonaci: "+nthValue);

}
   static int NonFibonacci(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$ mcs *.cs -out:main.exe                                                                                                                                                                                                                                         

sh-4.3$ mono main.exe                                                                                                                                                                                                                                                  

Enter the nth fibonacci value:                                                                                                                                                                                                                                         

5                                                                                                                                                                                                                                                                      

5th fibonaci: 5                                                                                                                                                                                                                                                        

sh-4.3$ mono main.exe                                                                                                                                                                                                                                                  

Enter the nth fibonacci value:                                                                                                                                                                                                                                         

8                                                                                                                                                                                                                                                                      

8th fibonaci: 21