Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
C
Java
def fibonacci_tabulation(n): if n == 0: return 0 elif n == 1: return 1 F = [0] * (n + 1) F[0] = 0 F[1] = 1 for i in range(2, n + 1): F[i] = F[i - 1] + F[i - 2] print(F) return F[n] n = 10 result = fibonacci_tabulation(n) print(f"\nThe {n}th Fibonacci number is {result}") #Python
#include
int fibonacciTabulation(int n) { if (n == 0) return 0; if (n == 1) return 1; int F[n + 1]; F[0] = 0; F[1] = 1; for (int i = 2; i <= n; i++) { F[i] = F[i - 1] + F[i - 2]; } for (int i = 0; i <= n; i++) { printf("%d ", F[i]); } printf("\n"); return F[n]; } int main() { int n = 10; int result = fibonacciTabulation(n); printf("\nThe %dth Fibonacci number is %d\n", n, result); return 0; } //C
public class Main { public static int fibonacciTabulation(int n) { if (n == 0) return 0; if (n == 1) return 1; int[] F = new int[n + 1]; F[0] = 0; F[1] = 1; for (int i = 2; i <= n; i++) { F[i] = F[i - 1] + F[i - 2]; } for (int num : F) { System.out.print(num + " "); } System.out.println(); return F[n]; } public static void main(String[] args) { int n = 10; int result = fibonacciTabulation(n); System.out.println("\nThe " + n + "th Fibonacci number is " + result); } } //Java
Python result:
C result:
Java result:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
The 10th Fibonacci number is 55
0 1 1 2 3 5 8 13 21 34 55
The 10th Fibonacci number is 55
0 1 1 2 3 5 8 13 21 34 55
The 10th Fibonacci number is 55