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.
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.
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.
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.
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.
- Advantages of C#
- C# vs. Java: What Are Its Main Differences
- Advantages of C# over Python
- First C# Program | Hello World
- Difference between Console.Write and Console.WriteLine in C#
- How do I create a MessageBox in C#?
- C# Comments
- How to reverse a string in C#
- Palindrome in C# with Examples
- C# Program to Print Number Triangle
- Get Integer Input from User in C# Console Application
- C# StringBuilder | Learn To Use C# StringBuilder Class
- C# HashMap (Dictionary)
- Ternary Operator (? :) in C# with Examples
- How To Sort Datatable in C#
- Struct Vs Class in C# | Differencees and Advantages
- Async And Await In C#
- IEnumerable in C# | Examples
- ref Vs out in C#
- How to remove item from list in C#?
- How to Add Items to a C# List
- C# StreamWriter Examples
- StreamReader in C# |Examples
- C# Map Example
- Static Method In C# | Examples
- How to convert an Enum to a String in C#
- How to convert a string to an enum in C#
- How to filter a list in C#?