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


Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2.  
  3. class functionexcercise
  4. {
  5. public static int Fibonaccisequence(int number)
  6. {
  7. int num1 = 0;
  8. int num2 = 1;
  9.  
  10. for (int i = 0; i < number; i++)
  11. {
  12. int temp = num1;
  13. num1 = num2;
  14. num2 = temp + num2;
  15. }
  16. return num1;
  17. }
  18. public static void Main()
  19. {
  20. Console.Write("Enter a number : ");
  21. int num = Convert.ToInt32(Console.ReadLine());
  22.  
  23. Console.WriteLine("The Fibonacci series of " + num + " numbers is :");
  24. for (int i = 0; i < num; i++)
  25. {
  26. Console.Write(Fibonaccisequence(i) + " ");
  27. }
  28. Console.ReadLine();
  29. }
  30. }

Result

C# language techstudy

     





Related Topics




Popular Posts