 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the nth value of a Fibonacci series using recursion in C#?
Create a method to get the nth value with recursion.
public int displayFibonacci(int n)
Call the method −
displayFibonacci(val)
On calling, the displayFibonacci() meyhod gets called and calculate the nth value using recursion.
public int displayFibonacci(int n) {    if (n == 0) {       return 0;    }    if (n == 1) {       return 1;    } else {       return displayFibonacci(n - 1) + displayFibonacci(n - 2);    } } Let us see the complete code −
Example
using System; public class Demo {    public static void Main(string[] args) {       Demo d = new Demo();       int val = 7;       int res = d.displayFibonacci(val);       Console.WriteLine("{0}th number in fibonacci series = {1}", val, res);    }    public int displayFibonacci(int n) {       if (n == 0) {          return 0;       }         if (n == 1) {          return 1;       } else {          return displayFibonacci(n - 1) + displayFibonacci(n - 2);       }    } }  Output
7th number in fibonacci series = 13
Advertisements
 