;

C# Program To Print A Fibonacci Series


Tutorialsrack 04/08/2019 C# Programs

In this C# program, we will learn how to write a program to print a Fibonacci series.

What is the Fibonacci Series?

In mathematics, the Fibonacci numbers, commonly denoted Fn form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

F0=0 and F1=1

And 

Fn=Fn-1 + Fn-2

Example of Fibonacci Series is  0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 and so on.

Here is the code of the program to print a Fibonacci series:

Code - C# Program To Print A Fibonacci Series
using System;

namespace TutorialsrackPrograms
{
    class Program
    {
        //Program to print a Fibonacci Series
        static void Main(string[] args)
        {
            int a = 0, b = 1, c = 0;
            Console.Write("Enter The Number: ");
            int len = Convert.ToInt16(Console.ReadLine());
            Console.WriteLine();
            Console.Write("Fibonacci Series:\t");
            Console.Write("{0} {1}", a, b);
            for (int i = 2; i < len; i++)
            {
                c = a + b;
                Console.Write(" {0}", c);
                a = b;
                b = c;
            }
            Console.Read();
        }
    }
}
Output

Enter The Number: 10

Fibonacci Series: 0  1  1  2  3  5  8  13  21  34


Related Posts



Comments

Recent Posts
Tags