AI:Prolog write a prolog pro to find factorial of given number.

We need to calculate factorials of any number given by user.There are some allready result have o factorials have result 1 and 1 factorials also result 0.The two predication are used in the proggram as go and fact(int,int).

We need to get input value that is identifies factorials value need.The basic formulas that is find the factorials are given belows:
  • Takes input from user.
  • decrease value of number.
  • The decrease value store other variable.
  • Then multiplication original value to the decrease value.
  • That's process until, we not get 1.
The answer is sore using other variable that multiplication process get result.The below program have some predication and clauses are given belows:
The predication are go and fact(int,int).And clauses that is go,fact(0,1),fact(1,A),fact(N,A).


predicates
               go
               fact(integer,integer)
clauses
               go:-
                              write("Enter the no.:"),
                              readint(N),
                              fact(N,A),
                              write("Factorial:",A,"\n").
               fact(0,A):-
                              A=1.
               fact(1,A):-
                              A=1.
               fact(N,A):-
                              B=N-1,
                              fact(B,S),
                              A=N*S.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       .