Sunday, 15 December 2013

C# program to find the Fibonacci Series



In this article we will show you how to write code for Fibonacci series using console application in c#.net using for loop:

Step1: Take a new console application using c# as a language in the Visual studio.
Step2: Declare a variable “int_No_of_term” for getting the Number of Term in the Fibonacci series.
Step3: Read an input from the User in this Variable.
Step4: Make an array for name “int_fibbo” of the size “int_No_of_term” for storing the fibonacci series values.
Step5: Assign the first and second value of the Fibonacci series in the array position 0 and 1 respectively. Print the first 2 number of the Fibonacci series.
Step6: now loop through till the last no in the series to calculate the next Fibonacci numbers using the logic nth series value= (n-1)th series value+ (n-2)th series value.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fibonaci
{
    class Program
    {

        static void Main()
        {
           
            int int_No_of_term;
            Console.WriteLine("Enter the number of terms for the fibonacci series you    
                           want to print?");
      
            int_No_of_term = int.Parse(Console.ReadLine());

            int[] int_fibbo = new int[int_No_of_term];

            int_fibbo[0] = 0;
            int_fibbo[1] = 1;


            //for printing the first value of the fibonacci series
            Console.WriteLine(int_fibbo[0]);
            //for printing the second value of the fibonacci series
            Console.WriteLine(int_fibbo[1]);

            for (int i = 2; i < int_No_of_term; i++)
            {
                int_fibbo[i] = int_fibbo[i - 1] + int_fibbo[i - 2];
                //for print the fibonacci series from 3rd position till nth position
                Console.WriteLine(int_fibbo[i]);
            }
            Console.ReadLine();
       }
    }



}

Output:-




No comments:

Post a Comment