Prolog program for printing Fibonacci series up to N numbers.


% Prolog program for printing fibonacci series upto n numbers------

% Alamgir Hossain, CSE, JUST
go:-
    write('Enter a number : '),read(N),nl,

    write('Fibonacci series for '),write(N),write(' elements is : '),nl,

    A is 0,

    B is 1,

    write(A),write(' '),write(B),write(' '),

    fibonacci(N,A,B).

fibonacci(N,A,B):-

    (       
        N<2, write('Complete');     
        C is A+B,
        write(C),write(' '),
        D is B,
        E is C,
        N1 is N-1,
        fibonacci(N1,D,E)
    ).   
% End of code
    % Output--------------
go.
Enter a number : 10.

Fibonacci series for 10 elements is :
0 1 1 2 3 5 8 13 21 34 55 Complete

No comments:

Post a Comment