Einführung in C#
Beispiel zur Berechnung der Fibonacci-Zahlenfolge
Leonardo Fibonacci (1170-1250) entdeckte eine Folge von Zahlen, die mit 1 und 1 beginnt.
Jede folgende Zahl entsteht als Summe der beiden Vorgänger.
Also: 1,1,2,3,5,8,13,21,34....
Hier ein Programm, welches die Folgen berechnet:
namespace ConsoleApplication_Fibonacci { class Program { static void Main(string[] args) { double x, x0, x1; int zahl, folge; char antwort; Console.WriteLine("Dieses Programm berechnet die Fibonacci-Folgen\n"); do { Console.Write("Geben Sie die Anzahl der zu berechnenden Folgen ein "); folge = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("\nFibonacci-Folge von 1 = \t 1"); x0 = 0; x1 = 1; zahl = 2; do { x = x0 + x1; x0 = x1; x1 = x; Console.WriteLine("Fibonacci-Folge von {0} = \t {1}", zahl, x); zahl++; } while (zahl <= folge); Console.WriteLine("\nNeue Berechnung? [j / n]"); antwort = Convert.ToChar(Console.ReadLine()); } while ((antwort == 'j') || (antwort == 'J')); } } }