Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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:

C# Sharp Exercises: Function : To calculate the Fibonacci number of a specific term

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 :

Flowchart: C# Sharp Exercises - Recursive Function : To calculate the Fibonacci number of a specific term

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.