Sponsored Ad

Friday, September 10, 2010

Fibonacci Program in C#

The Fibonacci series is generated by adding the current and previous numbers and generate the next number. and again sum the next number and prevois number and generate the next number.

The program is very simple and coded here using the for loop. Please feel free to comment in case of any problems.

Fibonacci Program in C#

 

Code:

using System;
using System.Text;
using System.Collections;
using System.Data;
namespace Console_App
{
    public class clsFactorial
    {      
        public static void Main()
        {
            try
            {
                Console.WriteLine("The fibonacci Series is:\n");
                    fibonacci();
            }
            catch(Exception ex)
            {
                //handle exception here
            }
            Console.ReadLine();           
        }

        static void fibonacci()
        {

            int Previous, Next;
            Previous = -1;
            Next = 1;
            int n;
            n = 20;
            int fib = 1;
            while (fib <= n)
            {
                fib = Previous + Next;
                Previous = Next;
                Next = fib;
                Console.Write(fib + " " );
            }
        }
    }
}

0 comments:

Post a Comment

Sponsored Ad

Website Update

Followers