xxxxxxxxxx
using System;
public class FibonacciExample
{
public static void Main(string[] args)
{
int n1=0,n2=1,n3,i,number;
Console.Write("Enter the number of elements: ");
number = int.Parse(Console.ReadLine());
Console.Write(n1+" "+n2+" "); //printing 0 and 1
for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
Console.Write(n3+" ");
n1=n2;
n2=n3;
}
}
}
xxxxxxxxxx
int Fibonacci(int n) => n == 0 ? 0 : n == 1 ? 1 : Fibonacci(n - 1) + Fibonacci(n - 2);
xxxxxxxxxx
// Print n fibonacci numbers
using System;
using System.Collections.Generic;
public class Program
{
public static void Main(){
var res = fibo(2);
foreach(var i in res) Console.Write(i+" ");
}
// Fibonacci function using recursion
public static List<int> fibo(int n){
List<int> res = new List<int>();
// Base case 1
if(n==1){
res.Add(0);
return res;
}
// Base case 2
if(n==2){
res.Add(0);
res.Add(1);
return res;
}
res.AddRange(fibo(n-1));
// ^ operator is used for negative indexing. This is supported only in C# 8.
res.Add(res[^1]+res[^2]);
return res;
}
}