C# Sharp Exercises: Function : To calculate the Fibonacci number of a specific term
C# Sharp Function: Exercise-12 with Solution
Write a program in C# Sharp to create a recursive function to calculate the Fibonacci number of a specific term.
Pictorial Presentation:
Sample Solution:
C# Sharp Code:
using System;
public class funcexer12
{
public static int Fib(int n1)
{
//if ( (n1 == 1) || (number == 2) )
if (n1 <=2)
return 1;
else
return Fib( n1 - 1 ) + Fib( n1 - 2 );
}
public static void Main()
{
int num;
Console.Write("\n\nRecursive Function : To calculate the Fibonacci number of a specific term :\n");
Console.Write("-------------------------------------------------------------------------------\n");
Console.Write("Enter a number: ");
num = Convert.ToInt32( Console.ReadLine() );
Console.WriteLine("\nThe Fibonacci of {0} th term is {1} \n", num, Fib(num));
}
}
Sample Output:
Recursive Function : To calculate the Fibonacci number of a specific term : ------------------------------------------------------------------------------- Enter a number: 10 The Fibonacci of 10 th term is 55
Flowchart :
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a program in C# Sharp to create a function to calculate the sum of the individual digits of a given number.
Next: C# Sharp Recursion Exercises.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework