w3resource logo

:

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.

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 

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus