Showing posts with label AI. Show all posts
Showing posts with label AI. Show all posts

20_09

Write a Prolog program to implement GCD of two numbers.

 Source Code :

 

gcd(X,0,X).

gcd(X,Y,Z):-

    R is mod(X,Y),

    gcd(Y,R,Z).

 

Output :

 

Prolog program to implement GCD of two numbers

Write a Prolog program to implement maxlist(List,Max) so that Max is the greatest number in the list of numbers List using cut predicate.

 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 :

 

Prolog program to implement maxlist(List,Max) so that Max is the greatest number in the list of numbers List using cut predicate

Write a Prolog program to implement reverse(List,ReversedList) that reverses lists.

 Source Code :

 

arev([],L,L).

arev([H|T],A,R):-

     arev(T,[H|A],R).

rev(L1,L2):-

     arev(L1,[],L2).

 

 

Output :

 

Prolog program to implement reverse(List,ReversedList) that reverses lists

Write a Prolog program to implement two predicates evenlength(List) and oddlength(List) so that they are true if their argument is a list of even or odd length respectively.

 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 :

Prolog program to implement two predicates evenlength(List) and oddlength(List) so that they are true if their argument is a list of even or odd length respectively


Write a Prolog program to implement sumlist(List,Sum) so that Sum is the sum of a given list of numbers List.

 Source Code :

 

listsum([],0).

listsum([H|T],R) :-

    listsum(T,S),

    R is H+S.

 

Output :

Prolog program to implement sumlist(List,Sum) so that Sum is the sum of a given list of numbers List


Write a Prolog program to implement maxlist(List,Max) so that Max is the greatest number in the list of numbers List.

 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 :

 

Prolog program to implement maxlist(List,Max) so that Max is the greatest number in the list of numbers List

Write a Prolog program to implement max(X,Y,Max) so that Max is the greater of two numbers X and Y.

 Source Code :

 

max(X,Y,R):-

 X>=Y ->

  R is X,

  write(R)

 ;

  R is Y,

  write(R).

  

Output :

Prolog program to implement max(X,Y,Max) so that Max is the greater of two numbers X and Y


Write a Prolog program to implement palindrome(List).

Source Code :

 

palind([]):- write('Palindrome').

palind([_]):- write('palindrome').

palind(L):-

 append([H|T],[H],L),

 palind(T)

 ;

 write('Not Palindrome').

 

Output :

Prolog program to implement palindrome(List)


Write a Prolog program to implement append for two lists.

 Source Code :

 

conc([],L,L).

conc([X|M],N,[X|Q]):-

 conc(M,N,Q).

 

 

Output :

Prolog program to implement append for two lists


Write a Prolog program, remove-nth(Before, After) that asserts the After list is the Before list with the removal of every n‘th item from every list at all levels.

 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 :

Write a Prolog program, remove-nth(Before, After) that asserts the After list is the Before list with the removal of every n‘th item from every list at all levels

 

Write a Prolog program to remove the Nth item from a list

 Source Code :

 

delet(1,[_|T],T).

delet(P,[X|Y],[X|R]):-

    P1 is P-1,

    delet(P1,Y,R).

 

Output :

Write a Prolog program to remove the Nth item from a list


Write a prolog program, insert_nth(item, n, into_list, result) that asserts that result is the list into_list with item inserted as the n‘th element into every list at all levels.

 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 :

Write a prolog program, insert_nth(item, n, into_list, result) that asserts that result is the list into_list with item inserted as the n‘th element into every list at all levels


Write a prolog program to calculate the nth Fibonacci number

 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 :

Write a prolog program to calculate the nth Fibonacci number


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

 

Write a prolog program to find the maximum of two numbers

 Source Code :

 

max(X,Y):-

( 

 X=Y ->

  write('Both are equal')

 ;

 X>Y ->

  (

  Z is X,

  write(Z)

  )

  ;

  (

  Z is Y,

  write(Z)

  )

).

 

Output :


Write a prolog program to find the maximum of two numbers

Write a prolog program to calculate the sum of two numbers.

Source Code :


sum(X,Y):-

    S is X+Y,

    write(S).


Output :

 

Write a prolog program to calculate the sum of two numbers

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...