Last active
August 30, 2017 01:27
-
-
Save Coding-Enthusiast/40bf7e9b7780eaa6b6f07f257acf2fe7 to your computer and use it in GitHub Desktop.
Find the closest Fibonacci number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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