Slider

C# Program to Find Fibonacci Series.

Write a C# program to generate a Fibonacci series of n terms. Steps to Print Fibonacci Series. Fibonacci Series of 10 terms: 0 1 1 2 3 5 8 13 21 34

Problem Statement: Write a C# program to generate a Fibonacci series of n terms. The program should display the first n numbers in the Fibonacci sequence.

Example:

Input: n = 10
Output: 
Fibonacci Series of 10 terms:
0 1 1 2 3 5 8 13 21 34

Steps to Print Fibonacci Series.

Below are steps that you need to follow to print a Fibonacci series in C# Sharp:

Step 1: Take an integer n as input, which represents the number of terms in the Fibonacci series.
Step 2: Initialize three variables: firstTerm and secondTerm to 0 and 1 (the initial terms of the Fibonacci sequence), and nextTerm to 0 (to hold the next term to be calculated).
Step 3: Display a header message, such as "Fibonacci Series of n terms."
Step 4: Use a for loop to generate the Fibonacci series. Start the loop from 0 and iterate n times.
Step 5: In each iteration of the loop, calculate the nextTerm as the sum of firstTerm and secondTerm.
Step 6: Update firstTerm and secondTerm to prepare for the next iteration. Set firstTerm to the value of secondTerm, and secondTerm to the value of nextTerm.

Step 7: Display the nextTerm as the current term in the Fibonacci sequence.

C# Sharp Code to Print Fibonacci Series.

// C-sharp code to print Fibonacci Series
using System;

namespace FibonacciSeries
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the number of terms: ");
            int n = Convert.ToInt32(Console.ReadLine());

            int firstTerm = 0, secondTerm = 1, nextTerm;

            Console.WriteLine($"Fibonacci Series of {n} terms:");
            for (int i = 0; i < n; i++)
            {
                if (i <= 1)
                    nextTerm = i;
                else
                {
                    nextTerm = firstTerm + secondTerm;
                    firstTerm = secondTerm;
                    secondTerm = nextTerm;
                }
                Console.Write(nextTerm + " ");
            }
        }
    }
}
Output:
Enter the number of terms: 8
Fibonacci Series of 8 terms:
0 1 1 2 3 5 8 13 

Similar articles:
0

No comments

Post a Comment

both, mystorymag

DON'T MISS

Nature, Health, Fitness
© all rights reserved
made with by AlgoLesson
Table of Contents