Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to print the first ten Fibonacci numbers using C#?
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. In C#, we can generate and print the first ten Fibonacci numbers using loops and basic arithmetic operations.
Understanding the Fibonacci Sequence
The Fibonacci sequence begins with 0 and 1. Each subsequent number is calculated by adding the two previous numbers together −
Algorithm
The algorithm follows these steps −
-
Initialize the first two Fibonacci numbers:
val1 = 0andval2 = 1 -
Print the first two numbers
-
Use a loop to calculate and print the remaining numbers by adding the previous two
-
Update the variables for the next iteration
Example
using System;
public class Demo {
public static void Main(string[] args) {
int val1 = 0, val2 = 1, val3, i, n;
n = 10;
Console.WriteLine("First ten Fibonacci numbers:");
Console.Write(val1 + " " + val2 + " ");
for(i = 2; i < n; ++i) {
val3 = val1 + val2;
Console.Write(val3 + " ");
val1 = val2;
val2 = val3;
}
Console.WriteLine();
}
}
The output of the above code is −
First ten Fibonacci numbers: 0 1 1 2 3 5 8 13 21 34
Using Arrays to Store Fibonacci Numbers
An alternative approach uses an array to store all Fibonacci numbers before printing them −
using System;
public class FibonacciArray {
public static void Main(string[] args) {
int n = 10;
int[] fib = new int[n];
fib[0] = 0;
fib[1] = 1;
for(int i = 2; i < n; i++) {
fib[i] = fib[i-1] + fib[i-2];
}
Console.WriteLine("First ten Fibonacci numbers using array:");
for(int i = 0; i < n; i++) {
Console.Write(fib[i] + " ");
}
Console.WriteLine();
}
}
The output of the above code is −
First ten Fibonacci numbers using array: 0 1 1 2 3 5 8 13 21 34
Using Recursion
A recursive approach calculates each Fibonacci number by calling itself −
using System;
public class FibonacciRecursive {
public static int Fibonacci(int n) {
if(n <= 1) {
return n;
}
return Fibonacci(n-1) + Fibonacci(n-2);
}
public static void Main(string[] args) {
Console.WriteLine("First ten Fibonacci numbers using recursion:");
for(int i = 0; i < 10; i++) {
Console.Write(Fibonacci(i) + " ");
}
Console.WriteLine();
}
}
The output of the above code is −
First ten Fibonacci numbers using recursion: 0 1 1 2 3 5 8 13 21 34
Comparison of Approaches
| Approach | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Iterative (Loop) | O(n) | O(1) | Large sequences, efficiency |
| Array-based | O(n) | O(n) | When all values need storing |
| Recursive | O(2^n) | O(n) | Small sequences, educational |
Conclusion
The iterative approach using a loop is the most efficient method for generating Fibonacci numbers in C#. It uses constant space and linear time complexity, making it suitable for both small and large sequences without the performance overhead of recursion.
