60 469
Assignments Done
98,1%
Successfully Done
In April 2018

# Answer to Question #1617 in C# for James

Question #1617
I want to write a code using the fibonacci series. When a user inserts a number it should give the numbers in between the sequence along with an option to conitinue or quit.
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace q1617
{
class Program
{
static int number;
//Get Fibonacci number before and after &quot;int Number&quot;
static int[] fibonacciBetween(int Number)
{
int before = 0;
int after = 1;
do
{
//Example:
//0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,
if (before &lt; Number && Number &lt; after) return new int[] { before, after, 0 }; // it for case when number = 22 (from 21 to 34)

if (before &lt; Number && Number == after) return new int[] { before, before + after, 1 }; // it for case when number = 34 (21 or 34, etc..)
int afterTMP = after;
after = before + after;
before = afterTMP;
} while (true);
}
static void Main(string[] args)
{
Console.Clear();
Console.Write(&quot;Enter number: &quot;);

if (number &lt; 1)
{
Console.WriteLine(&quot;It&#039;s a bad number&quot;);
return;
}

int[] between = fibonacciBetween(number);
Console.WriteLine(&quot;Number before: {0}&quot;, between[0]);
Console.WriteLine(&quot;Number after: {0}&quot;, between[1]);
Console.WriteLine(&quot;Press space for show next fibonacci number(other key for exit)&quot;);
ConsoleKeyInfo key = Console.ReadKey(true);
while (key.Key == ConsoleKey.Spacebar)
{
if (between[2] == 1)
{
between[0] = between[1] - between[0];
between[2] = 0;
}
int afterTMP = between[1];
between[1] = between[1] + between[0];
between[0] = afterTMP;
Console.Write(&quot; {0}&quot;, between[1]);
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!