20_09

Write a prolog program to calculate the factorial of a given number

 Source Code :

fact(0,1).

fact(N,F):-

(

 % The below is for +ve factorial.

 N>0 ->

 (

  N1 is N-1,

  fact(N1,F1),

  F is N*F1

 )

 ;

 % The below is for -ve factorial.

 N<0 ->

 (

  N1 is N+1,

  fact(N1,F1),

  F is N*F1

 )

).

 

Output :

Write a prolog program to calculate the factorial of a given number

 

No comments:

Post a Comment

Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...