Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Coding-Enthusiast
Last active August 30, 2017 01:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Coding-Enthusiast/40bf7e9b7780eaa6b6f07f257acf2fe7 to your computer and use it in GitHub Desktop.
Save Coding-Enthusiast/40bf7e9b7780eaa6b6f07f257acf2fe7 to your computer and use it in GitHub Desktop.
Find the closest Fibonacci number
using System;
using System.Collections.Generic;
namespace Fibonachi
{
class Program
{
static void Main(string[] args)
{
Console.Write("Number: ");
int i = int.Parse(Console.ReadLine());
Console.WriteLine("Closest Fibonachi is: {0}", ClosestFibonacci(i));
Console.ReadLine();
}
static int ClosestFibonacci(int num)
{
if (num < 1)
{
return 0;
}
else if (num == 1)
{
return 1;
}
else
{
List<int> Fibonachi = new List<int>();
Fibonachi.Add(1);
Fibonachi.Add(1);
while (true)
{
int index = Fibonachi.Count;
Fibonachi.Add(Fibonachi[index - 1] + Fibonachi[index - 2]);
if (Fibonachi[Fibonachi.Count - 1] > num)
{
break;
}
}
return Fibonachi[Fibonachi.Count - 2];
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment