Fibonacci Series
A Fibonacci series is a series of numbers where the next number is a sum of the previous two numbers.
For example, a Fibonacci series with the first two numbers of 1 and 2 is:
1, 2, 3, 5, 8, 13, 21, 34 etc.
Example:
using System; public class Example { public static void Main(string[] args) { int x=1, y=2, z, i, n; Console.Write("Enter the number of elements: "); n = int.Parse(Console.ReadLine()); Console.Write(x + " " + y + " "); for(i=2; i<n; ++i) { z = x + y; Console.Write(z + " "); x = y; y = z; } } } |
Output:
Explanation:
In the above example, we are printing a Fibonacci series starting with 1 and 2 in C#.