gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
write fibonacci in c#
- User
write fibonacci in c#
- Assistant
Here is an example of how you can write the Fibonacci series in C#:
using System; class Program { static void Main(string[] args) { Console.Write("Enter the number of terms: "); int n = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Fibonacci Series:"); // Print Fibonacci series for (int i = 0; i < n; i++) { Console.Write(Fibonacci(i) + " "); } Console.ReadLine(); } // Fibonacci function public static int Fibonacci(int n) { if (n <= 1) return n; return Fibonacci(n - 1) + Fibonacci(n - 2); } }In this code, the
Mainfunction takes input from the user for the number of terms to be displayed in the Fibonacci series. It then calls theFibonaccifunction to calculate each term in the series recursively. TheFibonaccifunction uses the concept of recursion where it returns the sum of the two previous terms until it reaches the base case ofn <= 1.