prolog program to calculate the factorial of a number using recursion


% Prolog program for finding factorial using recursion----

% Alamgir Hossain, CSE, JUST
fact(0,1).

fact(N,F):-

( 
    N>0 ->
    (
        N1 is N-1,
        fact(N1,F1),
        F is N*F1
    )
    ;
    write('N should be greater than 0.')

).
% Output----------

% fact(5,R).

%R = 120

No comments:

Post a Comment