
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
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
Get nth Value of 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