Fibonacci Series in C#

Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding numbers. The series starts with 0 and 1, and then the next number is the sum of the previous two numbers, i.e., 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on.

In C#, there are several ways to generate the Fibonacci series. Here are some possible methods:

Using a loop:

This is the simplest and most straightforward method to generate the Fibonacci series in C#. You can use a loop to iterate through the series and generate the numbers.

using System; public class Fibonacci { public static void FibonacciSeries(int n) { int firstNumber = 0, secondNumber = 1, nextNumber; Console.Write($"{firstNumber} {secondNumber} "); for (int i = 2; i < n; i++) { nextNumber = firstNumber + secondNumber; Console.Write($"{nextNumber} "); firstNumber = secondNumber; secondNumber = nextNumber; } } public static void Main(string[] args) { FibonacciSeries(10); } }
//Output: 0 1 1 2 3 5 8 13 21 34

In the above method, declare three variables - firstNumber, secondNumber, and nextNumber. Initialize firstNumber and secondNumber to 0 and 1 respectively, and then print these two numbers. Next, use a for loop to iterate through the series and generate the next numbers. Calculate the nextNumber by adding firstNumber and secondNumber, and then print it. Finally, assign secondNumber to firstNumber and nextNumber to secondNumber, so that you can use them in the next iteration.

Using recursion:

Recursion is another popular way to generate the Fibonacci series. In this method, we define a function that calls itself to generate the next numbers in the series.

using System; public class Program { public static void Main() { for (int i = 0; i < 10; i++) { Console.Write(FibonacciSeries(i) + " "); } } public static int FibonacciSeries(int n) { if (n <= 1) { return n; } else { return FibonacciSeries(n - 1) + FibonacciSeries(n - 2); } } }
//Output: 0 1 1 2 3 5 8 13 21 34

Above code defines a Main method that uses a loop to print the first 10 numbers of the Fibonacci sequence by calling the FibonacciSeries method. The FibonacciSeries method is a recursive function that calculates the n-th number of the Fibonacci sequence.


GENERATE FibonacciSeries NUMBERS IN c#

Using memoization:

Memoization is a technique that involves storing the results of expensive function calls and returning the cached result when the same inputs occur again. This can significantly improve the performance of the function, especially when it is called multiple times with the same inputs.

using System; public class Program { public static void Main() { for (int i = 0; i < 10; i++) { Console.Write(FibonacciSeries(i) + " "); } } public static int FibonacciSeries(int n) { int[] memo = new int[n + 1]; return FibonacciSeries(n, memo); } public static int FibonacciSeries(int n, int[] memo) { if (n <= 1) return n; else if (memo[n] > 0) return memo[n]; else { memo[n] = FibonacciSeries(n - 1, memo) + FibonacciSeries(n - 2, memo); return memo[n]; } } }
//Output: 0 1 1 2 3 5 8 13 21 34

In the above code, the Main method calls the FibonacciSeries method with argument values 0 through 9 to print the first 10 numbers of the Fibonacci series. The FibonacciSeries method takes an additional memo array parameter to implement memoization, which is a technique to optimize recursive function performance by avoiding redundant calculations.