Fibonacci series in c#.net
In this article, I will explain how we can calculate Fibonacci series in c#.net.
Introduction
In this article, I have describe a C# program that accept the number for Fibonacci series and print the series. The concept of Fibonacci series is very simple. It start with 0 and 1 and next term is the sum of the previous two terms. The sequence of Fibonacci series is following.
0 1 3 5 8 13….
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
int p = 0;
int q = 1;
int n,r;
Console.WriteLine("--------Please enter the no. for fibonacci series--------");
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("-----The series is-------");
Console.WriteLine(p);
Console.ForegroundColor = ConsoleColor.Green; // Change the ForegroundColor
for (int i = 1; i <= n; i++)
{
r = p + q;
p = q;
q = r;
Console.WriteLine(r);
}
Console.ReadKey();
}
}
}
The output of above program