w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: Find the Fibonacci numbers for a n numbers of series

C# Sharp Recursion: Exercise-10 with Solution

Write a program in C# Sharp to find the Fibonacci numbers for a n numbers of series using recursion.

C# Sharp: Find the Fibonacci numbers for a n numbers of series

Sample Solution:-

C# Sharp Code:

using System;

class RecExercise10
{
    public static int FindFibonacci(int n)
    {
	int p = 0;
	int q = 1;
	for (int i = 0; i < n; i++)
	{
	    int temp = p;
	    p = q;
	    q = temp + q;
	}
	return p;
    }

    static void Main()
    {
        Console.WriteLine("\n\n Recursion : Find the Fibonacci numbers for a n numbers of series :");
		Console.WriteLine("-----------------------------------------------------------------------");
	
		Console.Write(" Input number of terms for the Fibonacci series : ");
        int n1 = Convert.ToInt32(Console.ReadLine());
        Console.Write("\n The Fibonacci series of {0} terms is : ",n1);
	    for (int i = 0; i < n1; i++)
	        {
	            Console.Write("{0} ",FindFibonacci(i));
	        }
	Console.ReadKey();
    }
}

Sample Output:

 Recursion : Find the Fibonacci numbers for a n numbers of series :                                           
-----------------------------------------------------------------------                                       
 Input number of terms for the Fibonacci series : 10                                                          
                                                                                                              
 The Fibonacci series of 10 terms is : 0 1 1 2 3 5 8 13 21 34 

Flowchart :

Flowchart: C# Sharp Exercises - Find the Fibonacci numbers for a n numbers of series

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a program in C# Sharp to find the factorial of a given number using recursion.
Next: Write a program in C# Sharp to generate all possible permutations of an array using recursion.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming