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 display the n number Fibonacci sequence

C# Sharp Function: Exercise-8 with Solution

Write a program in C# Sharp to create a function to display the n number Fibonacci sequence.

Pictorial Presentation:

C# Sharp Exercises: Function: To display the n number Fibonacci sequence

Sample Solution:

C# Sharp Code:

using System;
class funcexer8
{
    public static int Fibo(int nno)
    {
	int num1 = 0;
	int num2 = 1;
	
	for (int i = 0; i < nno; i++)
	{
	    int temp = num1;
	    num1 = num2;
	    num2 = temp + num2;
	}
	return num1;
    }
    public static void Main()
    {
	  Console.Write("\n\nFunction : To display the n number Fibonacci series :\n");
      Console.Write("------------------------------------------------------------\n");
	  Console.Write("Input number of Fibonacci Series : ");
      int n= Convert.ToInt32(Console.ReadLine());
      
	  Console.WriteLine("The Fibonacci series of "+n+" numbers is :");	  
	for (int i = 0; i < n; i++)
	{
	    Console.Write(Fibo(i)+"  ");
	}
	Console.WriteLine();
    }
}

Sample Output:

Function : To display the n number Fibonacci series :                                                         
------------------------------------------------------------                                                  
Input number of Fibonacci Series : 10                                                                         
The Fibonacci series of 10 numbers is :                                                                       
0  1  1  2  3  5  8  13  21  34

Flowchart:

Flowchart: C# Sharp Exercises - Function: To display the n number Fibonacci series

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 result of raising an integer number to another.
Next: Write a program in C# Sharp to create a function to check whether a number is prime or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.