using System; using System.Collections.Generic; using System.Linq; namespace Fibonacci { class Program { static void Main(string[] args) { List<int> number = new List<int>(); checkInputData(ref number); if (checkFibonacci(number)) Console.WriteLine("is fibonacci sequence"); else Console.WriteLine("is not fibonacci sequence"); } static void checkInputData(ref List<int> number) { Console.Write("Enter fibonacci sequence: "); do { number = Console.ReadLine().Split().Select(int.Parse).ToList(); if (number[0] != 0 && number[1] != 1 || number[0] != 1 && number[1] != 1 ) Console.Write("ERROR! Enter again: "); } while (number[0] != 0 && number[1] != 1 || number[0] != 1 && number[1] != 1); } static bool checkFibonacci(List<int> number) { bool result = true; int sum = 0; for (int i = 0, j = 2; i < number.Count - 2; i++, j++) { sum = number[i] + number[i + 1]; if (number[j] != sum) { result = false; break; } } return result; } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter