Coding Schools


 
Python | C Sharp | Azure AI | HTML | JavaScript | CSS | SQL Server
OOPS in C#
C# Data Types
Boxing and Unboxing in C#
Garbage Collection in C#
C# - Conditional Statements
C# - Loops
Interfaces in C#
Generics in C#
Collections in C#
C# 8.0 new features
C# Singleton Design Pattern
C# Factory Design Pattern
LINQ in C#
C# - Program to Find Prime Numbers
C# - Fibonacci Sequence
C# - Factorial of a number
C# - Recursive methods
C# - Anonymous Methods
C# - String Methods
C# TDD - XUnit
C# TDD - NUnit
C# Multiton Design Pattern
C# Facade Design Pattern
C# Abstract Factory Design Pattern
C# Decorator Design Pattern
C# Composite Design Pattern

C# - Fibonacci Sequence



Here is a simple C# program that calculates and prints the Fibonacci sequence up to a given number of terms:

csharp
using System;

class Program
{
    static void Main()
    {
        int numberOfTerms = 10;

        Console.WriteLine("Fibonacci Sequence up to {0} terms:", numberOfTerms);
        for (int i = 0; i < numberOfTerms; i++)
        {
            Console.WriteLine(Fibonacci(i));
        }
    }

    // Method to calculate the nth Fibonacci number
    static int Fibonacci(int n)
    {
        if (n <= 1)
        {
            return n;
        }
        return Fibonacci(n - 1) + Fibonacci(n - 2);
    }
}

Explanation:

  1. Main Method:

    • Defines the number of terms for the Fibonacci sequence.

    • Loops through each term up to the specified number and prints the result from the Fibonacci method.

  2. Fibonacci Method:

    • Uses a recursive approach to calculate the Fibonacci number.

    • If n is 0 or 1, it returns n (the base cases).

    • Otherwise, it returns the sum of the previous two Fibonacci numbers.

Note:

The recursive approach is simple but not the most efficient for large numbers because it involves a lot of redundant calculations. For better performance, a more efficient iterative approach can be used:

csharp
using System;

class Program
{
    static void Main()
    {
        int numberOfTerms = 10;

        Console.WriteLine("Fibonacci Sequence up to {0} terms:", numberOfTerms);
        PrintFibonacciIterative(numberOfTerms);
    }

    // Method to print Fibonacci sequence using an iterative approach
    static void PrintFibonacciIterative(int numberOfTerms)
    {
        int first = 0, second = 1;

        for (int i = 0; i < numberOfTerms; i++)
        {
            Console.WriteLine(first);

            int next = first + second;
            first = second;
            second = next;
        }
    }
}

Explanation of the Iterative Approach:

  1. PrintFibonacciIterative Method:

    • Initializes the first two Fibonacci numbers first and second.

    • Iteratively calculates the next Fibonacci number by summing the previous two.

    • Updates the previous two Fibonacci numbers for the next iteration.

This iterative approach avoids the overhead of recursion and is more efficient, especially for larger sequences. Let me know if there's anything specific you'd like to modify or further explore!




All rights reserved | Privacy Policy | Sitemap