67 178
Assignments Done
99,3%
Successfully Done
In November 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.
Expert's answer
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 "int Number"
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 < Number && Number < after) return new int[] { before, after, 0 }; // it for case when number = 22 (from 21 to 34)

if (before < 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("Enter number: ");
number = Convert.ToInt32(Console.ReadLine());

if (number < 1)
{
Console.WriteLine("It's a bad number");
Console.ReadKey();
return;
}

int[] between = fibonacciBetween(number);
Console.WriteLine("Number before: {0}", between[0]);
Console.WriteLine("Number after: {0}", between[1]);
Console.WriteLine("Press space for show next fibonacci number(other key for exit)");
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(" {0}", between[1]);
key = Console.ReadKey(true);
}
}
}

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!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions