June 24, 2020

Fibonacci Series in C#

Write a program in C# to print Fibonacci series.

In case of fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of fibonacci series are 0 and 1.
Write a program in C# to print Fibonacci series.

Sample Solution

C# Sharp Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace fibonaci
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, n1, f1 = 0, f2 = 1, f3 = 0;
            Console.Write("Enter the Limit : ");
            n1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(f1);
            for (i = 0; i <= n1; i++)
            {
                f3 = f1 + f2;
                Console.WriteLine(f3);
                f1 = f2;
                f2 = f3;
            }
            Console.ReadLine();

        }
    }
}

Sample Output
Write a program in C# to print Fibonacci series.









No comments:

Post a Comment