Prolog program to find the Factorial of a number

  • Find the Factorial of a number
  • predicates
    	go
    	fact(integer,integer)
    clauses
    	go:-
    	write("give the no:"),
    	readint(X),
    	Z=1,
    	fact(X,Z).
    	
    	fact(1,Z):-
    	write("the result:",Z).
    	
    	fact(X,Z):-
    	Y=Z*X,
    	XX=X-1,
    	fact(XX,Y).
    About these ads

    2 thoughts on “Prolog program to find the Factorial of a number

    1. What about if we have the factorial of a number and we want to find in which number it belongs? For example i have 120 and want prolog to tell me that 120 is the factorial of 5.

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s