Function Fibonacci - C# Programming Exercise

In this C# exercise, you are asked to write a program that uses recursion to calculate a number in the Fibonacci series. The Fibonacci series is a sequence of numbers where the first two elements are 1, and for the subsequent elements, each one is the sum of the two preceding ones. For example, the first numbers in the series are 1, 1, 2, 3, 5, 8, 13, etc. This exercise is a great way to learn and apply the concept of recursion in C#. Instead of using iterative structures, you will need to implement a function that recursively calculates the number at the desired position in the series. Through this exercise, you will gain a deeper understanding of how to use recursion to solve mathematical problems and work with numerical sequences in C#.

 Category

Functions

 Exercise

Function Fibonacci

 Objective

Write a C# program that uses recursion to calculate a number in the Fibonacci series (in which the first two items are 1, and for the other elements, each one is the sum of the preceding two).

 Write Your C# Exercise

// Importing the System namespace to access basic system functions
using System;

class Program
{
    // Recursive function to calculate the Fibonacci number at a given position
    public static int Fibonacci(int n)
    {
        // Base case: The first and second Fibonacci numbers are 1
        if (n == 1 || n == 2)
        {
            return 1;
        }

        // Recursive case: The Fibonacci number is the sum of the previous two Fibonacci numbers
        return Fibonacci(n - 1) + Fibonacci(n - 2);
    }

    // Main method where the Fibonacci function is called
    public static void Main()
    {
        // Declare an integer for the position in the Fibonacci series
        int position = 6; // Example: We want to calculate the 6th Fibonacci number

        // Call the recursive Fibonacci function and print the result
        Console.WriteLine("The Fibonacci number at position {0} is: {1}", position, Fibonacci(position));
    }
}

 Share this C# exercise

 More C# Programming Exercises of Functions

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Function modify a letter in a string

    In this exercise of C#, you will learn how to create a function named ChangeChar that modifies a letter at a specific position (starting from 0) in a st...

  •  Function IsPrimeTarea

    In this exercise of C#, you will learn how to create a function named IsPrime that receives an integer number and returns true if the number is p...

  •  Function Parameters of Main, Sum

    In this exercise of C#, you will learn how to create a program named sum that receives two integer numbers from the command line and displays their sum. This t...

  •  Function SumDigits

    In this exercise of C#, you will learn how to create a function named SumDigits that receives a number and returns the result of summing its digits. Thi...

  •  Function Factorial

    In this exercise of C#, you will learn how to create a recursive function to calculate the factorial of a number. The factorial of a number is ex...

  •  Function Parameters of Main, Reverse

    In this exercise of C#, you will learn how to create a program named reverse that receives several words from the command line and display...