Source Code :
gcd(X,0,X).
gcd(X,Y,Z):-
R is mod(X,Y),
gcd(Y,R,Z).
Output :
In cs-assign blog you can find various types of computer program. Such as, programs in C, Java, Prolog etc.
Source Code :
max([H],H).
max([H|T],R):-
max(T,M1),
H>=M1,
R is H,!.
max([H|T],R):-
max(T,M1),
H<M1,
R is M1.
Output :
Source Code :
arev([],L,L).
arev([H|T],A,R):-
arev(T,[H|A],R).
rev(L1,L2):-
arev(L1,[],L2).
Output :
Source Code :
evenlength:-
write('true --> even').
oddlength:-
write('true --> odd').
oddeven([_H|T]):-
length(T,L),
L>=0 ->
(
L1 is L+1,
L2 is mod(L1,2),
L2=:=0 ->
evenlength
;
oddlength
).
Output :
Source Code :
maxlist([H|T],R):-
length(T,L),
L>0 ->
(
maxlist(T,R1),
(
H > R1 ->
R is H
;
R is R1
)
)
;
R is H.
Output :
Source Code :
palind([]):- write('Palindrome').
palind([_]):- write('palindrome').
palind(L):-
append([H|T],[H],L),
palind(T)
;
write('Not Palindrome').
Output :
Source Code :
/* Delete a number in the list */
delte(1,[_|T],T).
delte(P,[X|Y],[X|R]):-
P1 is P-1,
delte(P1,Y,R).
/* Delete before and after */
del(P,L,R):-
length(L,L1),
(
P=:=1 ->
P3 is P+1,
delte(P3,L,R)
;
P=:=L1 ->
P3 is P-1,
delte(P3,L,R)
;
P1 is P-1,
delte(P1,L,R1),
/* Delete before */
delte(P,R1,R)
/* Delete after */
).
Output :
Source Code :
mem(X,[X|_]).
mem(X,[_|T]):- mem(X,T).
insert(L,[_X|_Y],[L|_]).
insert(L,P,[X|Y],[X|M]):-
P>1,
P1 is P-1,
insert(L,P1,Y,M).
insert(L,1,[X|Y],M):- append([L],[X|Y],M).
Output :
Source Code :
fib(0, 1) :- !.
fib(1, 1) :- !.
fib(N, F) :-
N > 1,
N1 is N-1,
N2 is N-2,
fib(N1, F1),
fib(N2, F2),
F is F1+F2.
Output :
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 :
Source Code :
max(X,Y):-
(
X=Y ->
write('Both are equal')
;
X>Y ->
(
Z is X,
write(Z)
)
;
(
Z is Y,
write(Z)
)
).
Output :
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...