Monday, July 17, 2023

4. Write a prolog program to calculate the nth Fibonacci number.

 4. Write a prolog program to calculate the nth Fibonacci number. 


fib(0, 1) :- !.

fib(1, 1) :- !.

fib(N, F) :-        N > 1,        N1 is N-1,        N2 is N-2,        fib(N1, F1),        fib(N2, F2),        F is F1+F2.



OUTPUT:

fib(5N.
N = 8

No comments:

Post a Comment