C# Basic
C# Interview Programs
- C# Interview Programs
- C# Abundant Number
- C# Amicable Number
- C# Armstrong Number
- C# Average of N Numbers
- C# Automorphic Number
- C# Biggest of three numbers
- C# Binary to Decimal
- C# Common Divisors
- C# Composite Number
- C# Condense a Number
- C# Cube Number
- C# Decimal to Binary
- C# Decimal to Octal
- C# Disarium Number
- C# Even Number
- C# Evil Number
- C# Factorial of a Number
- C# Fibonacci Series
- C# GCD
- C# Happy Number
- C# Harshad Number
- C# LCM
- C# Leap Year
- C# Magic Number
- C# Matrix Addition
- C# Matrix Division
- C# Matrix Multiplication
- C# Matrix Subtraction
- C# Matrix Transpose
- C# Maximum Value of an Array
- C# Minimum Value of an Array
- C# Multiplication Table
- C# Natural Number
- C# Number Combination
- C# Odd Number
- C# Palindrome Number
- C# Pascalβs Triangle
- C# Perfect Number
- C# Perfect Square
- C# Power of 2
- C# Power of 3
- C# Pronic Number
- C# Prime Factor
- C# Prime Number
- C# Smith Number
- C# Strong Number
- C# Sum of Array
- C# Sum of Digits
- C# Swap Two Numbers
- C# Triangular Number
C# Program to Display Fibonacci Series
Photo Credit to CodeToFun
π Introduction
The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1.
This series has widespread applications in various fields, including mathematics and computer science.
In this tutorial, we'll explore a C# program that generates and displays the Fibonacci series.
π Example
Let's dive into the C# code that generates and displays the Fibonacci series.
using System;
class Program {
// Function to display Fibonacci series up to n terms
static void DisplayFibonacci(int n) {
int first = 0, second = 1, next;
Console.Write($"Fibonacci series up to {n} terms: ");
for (int i = 0; i < n; ++i) {
Console.Write($"{first}, ");
next = first + second;
first = second;
second = next;
}
Console.WriteLine();
}
// Driver program
static void Main() {
// Replace this value with the desired number of terms
int terms = 10;
// Call the function to display Fibonacci series
DisplayFibonacci(terms);
}
}
π» Testing the Program
To test the program with a different number of terms, simply replace the value of the terms variable in the Main method.
Fibonacci series up to 10 terms: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
Compile and run the program to see the Fibonacci series up to the desired number of terms.
π§ How the Program Works
- The program defines a function DisplayFibonacci that takes the number of terms (n) as input and prints the Fibonacci series up to n terms.
- Inside the function, it initializes variables first and second with 0 and 1, respectively.
- It then uses a loop to calculate and print the Fibonacci series up to the specified number of terms.
- The loop updates the values of first and second in each iteration to generate the next Fibonacci number.
π§ Understanding the Concept of Fibonacci Series
The Fibonacci series starts with 0 and 1. Each subsequent number in the series is the sum of the two preceding ones. The series begins as follows:
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
π’ Optimizing the Program
While the provided program is straightforward, consider exploring and implementing optimizations for larger Fibonacci series, such as using memoization techniques to avoid redundant calculations.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (C# Program to Display Fibonacci Series), please comment here. I will help you immediately.