public class Fibonacci {
public static List<Integer> getFibonacciNumbers(Integer n) {
List<Integer> fibonacciNumbers = new ArrayList<Integer>();
fibonacciNumbers.add(0);
fibonacciNumbers.add(1);
List<Integer> fibonacciNumbers = new ArrayList<Integer>();
fibonacciNumbers.add(0);
fibonacciNumbers.add(1);
for (int i = 2; i < n; i++) {
fibonacciNumbers.add(fibonacciNumbers.get(i - 1) + fibonacciNumbers.get(i - 2));
}
return fibonacciNumbers;
fibonacciNumbers.add(fibonacciNumbers.get(i - 1) + fibonacciNumbers.get(i - 2));
}
return fibonacciNumbers;
}
public static void displayFibonacciNumbers(Integer n) {
List<Integer> fibonacciNumbers = getFibonacciNumbers(n);
for (int i = 0; i < fibonacciNumbers.size(); i++) {
System.debug(fibonacciNumbers.get(i));
}
}
}
Display the Fibonacci series using Apex code #inSalesforce
This code first creates a class called Fibonacci that has two static methods: getFibonacciNumbers() and displayFibonacciNumbers().
The getFibonacciNumbers() method takes an integer as input and returns a list of Fibonacci numbers up to that number. The code for this method is the same as the code that I showed you earlier.
The displayFibonacciNumbers() method takes an integer as input and displays the Fibonacci numbers up to that number. The code for this method first calls the getFibonacciNumbers() method to get the list of Fibonacci numbers. Then, it iterates through the list and prints each Fibonacci number to the console.
To use this code, you would first need to create a new Apex class and paste the code into the class. Then, you would need to call the displayFibonacciNumbers() method, passing in the number of Fibonacci numbers that you want to display. For example, the following code would display the first 10 Fibonacci numbers:
Fibonacci.displayFibonacciNumbers(10);
This code would print the following numbers to the console: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
Comments
Post a Comment