w3resource logo


C# Sharp Programming Exercises

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

Secondary Nav

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.
Test Data :
Input number of terms for the Fibonacci series : 10
Expected Output
:
The Fibonacci series of 10 terms is : 0 1 1 2 3 5 8 13 21 34

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();
    }
}

C# Sharp Practice online:


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();
    }
}

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program