Search This Blog

Loading...

Aug 19, 2010

How to print Fibonacci series in c# Using For Loop


Sponsored Links:

image

Code Sample:

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

namespace fibseries
{

class Program
    {
        static void Main(string[] args)
        {
            int num; 
            Console.WriteLine("Please Enter a number:"); 
             num = Convert.ToInt32(Console.ReadLine()); 
             Console.WriteLine("\n"); 
             Console.WriteLine("The first " + num + " number(s) in the fibonacci series are: ");
             FibonacciSeries(num); 
             Console.Read(); 
        }
        // Fibonacci Code
        public static int FibonacciSeries(int n) 
        { 
             int previous = -1; 
             int next = 1; 
             for (int i = 0; i < n; i++) 
             { 
                 int sum = next + previous; 
                 previous = next; 
                 next = sum; 
                 Console.WriteLine(next); 
             } 
             return next;    
         } 

    }

Follow by Email