Fibonacci Generator Method in Apex

The Fibonacci sequence is one of the most well-known sequences in mathematics. Each number in the series is the sum of the two before it starting with 0 and 1. It goes like this:
0, 1, 1, 2, 3, 5, 8, 13, 21, … and so on.

In this blog post, we’ll build a simple Apex method that generates a list of Fibonacci numbers up to a certain limit. If you’re just learning Apex or want to practice logic and looping, this example is perfect for you.

🧠 What Are We Trying to Do?

We want to:

  • Accept a number endNumber as input

  • Generate Fibonacci numbers starting from 0

  • Stop when the next number would be greater than or equal to endNumber

  • Return the series as a list of integers

🔍 Code Explanation

 
public with sharing class ApexUseCaseFourteen {
    public static List<Integer> returnFibonacciSequence(Integer endNumber){

We define a class called ApexUseCaseFourteen and a method named returnFibonacciSequence that accepts an integer input the number where we want the Fibonacci series to stop.

        List<Integer> finalResult = new List<Integer>();
        Integer m = 0, n = 1; 
        finalResult.add(m);
        finalResult.add(n);

We declare and initialize two numbers: m = 0 and n = 1, the first two numbers of the Fibonacci sequence. We also create a list called finalResult and add these starting values to it.

        for(Integer i = 0; i <= endNumber; i++){
            Integer nextNumber = m + n;
            if(nextNumber >= endNumber){
                break;
            }

Here we use a for loop (although the index i isn’t actually needed here — more on that later). In each loop:

  • We calculate the next number in the sequence as m + n

  • If this next number is greater than or equal to endNumber, we break the loop

 
            m = n;
            n = nextNumber;
            finalResult.add(nextNumber);
        }
        return finalResult;
    }
}

After generating each new number, we:

  • Move n into m

  • Move the new number into n

  • Add the number to our result list

Once we’re done, we return the full list of Fibonacci numbers!

✅ Why This Is Useful

This example is a great beginner project to practice:

  • Working with lists in Apex

  • Using loops and conditional logic

  • Thinking algorithmically

You can plug this into:

  • Math-based learning apps

  • Coding challenges in Salesforce training

  • Fun demonstrations for algorithm interviews

You could also modify the method to:

  • Return the first N Fibonacci numbers

  • Use recursion (carefully!)

  • Format results as strings

🧾 Final Code Snippet

public with sharing class ApexUseCaseFourteen {
    public static List<Integer> returnFibonacciSequence(Integer endNumber){
        
        List<Integer> finalResult = new List<Integer>();
        Integer m = 0, n = 1; 
        finalResult.add(m);
        finalResult.add(n);
        for(Integer i = 0; i <= endNumber; i++){
            Integer nextNumber = m + n;
            if(nextNumber >= endNumber){
                break;
            }
            m = n;
            n = nextNumber;
            finalResult.add(nextNumber);
        }
        return finalResult;
    }
}

🎥 Watch It in Action!

Want to see this Fibonacci generator in action? Watch my short demo video where I run this Apex code in the Developer Console and explain each step live!

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)