Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
C
Java
def nth_fibo(n): if n==0: return 0 if n==1: return 1 F = [None] * (n + 1) F[0] = 0 F[1] = 1 for i in range(2, n + 1): F[i] = F[i - 1] + F[i - 2] return F[n] n = 6 result = nth_fibo(n) print(f"The {n}th Fibonacci number is {result}") #Python
#include
int nthFibo(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]; } return F[n]; } int main() { int n = 6; int result = nthFibo(n); printf("The %dth Fibonacci number is %d\n", n, result); return 0; } //C
public class Main { public static int nthFibo(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]; } return F[n]; } public static void main(String[] args) { int n = 6; int result = nthFibo(n); System.out.println("The " + n + "th Fibonacci number is " + result); } } //Java
Python result:
C result:
Java result:
The 6th Fibonacci number is 8
The 6th Fibonacci number is 8
The 6th Fibonacci number is 8