Large Fibonacci Numbers in C#

To display large Fibonacci numbers in C#, we need to handle numbers that exceed the range of standard integer types. The Fibonacci sequence grows exponentially, so after just 40-50 terms, the values become too large for int or even long data types.

For truly large Fibonacci numbers, we use the BigInteger class from System.Numerics, which can handle arbitrarily large integers without overflow.

Syntax

Following is the basic syntax for generating Fibonacci numbers −

int val1 = 0, val2 = 1, val3;
for (int i = 2; i < n; i++) {
   val3 = val1 + val2;
   val1 = val2;
   val2 = val3;
}

For large numbers, use BigInteger

using System.Numerics;
BigInteger val1 = 0, val2 = 1, val3;

Using Standard Integer Types

This approach works for smaller Fibonacci numbers but will overflow for large values −

using System;

public class Demo {
   public static void Main(string[] args) {
      int val1 = 0, val2 = 1, val3, i, n;
      n = 40;
      Console.WriteLine("Displaying Fibonacci series:");
      Console.Write(val1 + " " + val2 + " ");
      for (i = 2; i 

The output of the above code is −

Displaying Fibonacci series:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986

Using BigInteger for Large Fibonacci Numbers

For truly large Fibonacci numbers, the BigInteger class handles values of any size −

using System;
using System.Numerics;

public class LargeFibonacci {
   public static void Main(string[] args) {
      BigInteger val1 = 0, val2 = 1, val3;
      int n = 100;
      Console.WriteLine("First " + n + " Fibonacci numbers:");
      Console.WriteLine("F(0) = " + val1);
      Console.WriteLine("F(1) = " + val2);
      
      for (int i = 2; i 

The output shows the first 100 Fibonacci numbers, including extremely large values −

First 100 Fibonacci numbers:
F(0) = 0
F(1) = 1
F(2) = 1
F(3) = 2
F(4) = 3
F(5) = 5
...
F(95) = 31940434634990099905
F(96) = 51680708854858323072
F(97) = 83621143489848422977
F(98) = 135301852344706746049
F(99) = 218922995834555169026

Optimized Version with Method

Here's a more organized approach using a method to calculate specific Fibonacci numbers −

using System;
using System.Numerics;

public class FibonacciCalculator {
   public static BigInteger GetFibonacci(int n) {
      if (n 

The output of the above code is −

F(50) = 12586269025
Length: 11 digits

F(100) = 354224848179261915075
Length: 21 digits

F(200) = 280571172992510140037611932413038677189525
Length: 42 digits

Key Considerations

Data Type Maximum Safe Fibonacci Index Use Case
int ~46 Small Fibonacci numbers
long ~93 Medium-sized Fibonacci numbers
BigInteger No practical limit Large Fibonacci numbers

Conclusion

For large Fibonacci numbers in C#, use the BigInteger class to avoid integer overflow. While standard integer types are sufficient for small sequences, BigInteger allows you to calculate Fibonacci numbers of any size, making it essential for mathematical applications requiring precision with large numbers.

Updated on: 2026-03-17T07:04:35+05:30

542 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements