You are on page 1of 36

ALPHA COLLEGE OF ENGINEERING AND TECHNOLOGY

Department of Computer Engineering


Semester – VIII
Lab Manual For
AIRTIFICIAL INTELLIGENCE(2180703)

Student Name: - _______________________________


Enroll Number: -_______________________________

INDEX
Sr. Title Pg. Start Complete Sign Marks
No. No. Date Date
1 Write a program to implement Tic-Tac- 1
Toe game problem
2 Write a program to implement BFS (for 8 9
puzzle problem or Water Jug problem or
any AI search
problem)
3 Write a program to implement DFS (for 8 13
puzzle problem or Water Jug problem or
any AI search
problem)
4 Write a program to implement Single 17
Player Game (Using Heuristic Function)
5 Write a program to Implement A* 19
Algorithm
6 6.1 Enter the facts for the following 21
predicates for a Turbo Prolog
program
likes (person, drink)
enjoy (person, hobby)
plays (person, instrument)
6.2 Write a Prolog program for your 22
Family Tree
6.3 Develop a medical diagnostic expert 27
system using Prolog
7 7.1 Find out sum of numbers from 1 to 28
10
7.2 Print the following pattern: 29
*,**,***,****
7.3 Find out factorial of any given 30
number
8 8.1 Write a program to check whether a 31
year is leap year or not
8.2 Write a turbo Prolog program to 32
display integer numbers from 1 to
100 on
successive lines. However when a
number is divisible by 3 or 7,
display “Buzz 3”or Buzz 7”
respectively next to the number. If
the number is the number. The
output should therefore be as follows
9 Write a prolog program to generate 34
Fibonacci Series
10 Write the Conceptual Dependency for 35
following statements.
(a) John gives Mary a book
(b) John gave Mary the book yesterday
AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
Practical 1
#include<stdio.h>
#include<conio.h>

void Board();
void PlayerX();
void PlayerO();
void Player_win();
void check();
int win=0,wrong_X=0,wrong_O=0,chk=0;

char name_X[30];
char name_O[30];
int pos_for_X[3][3];
int pos_for_O[3][3];
int pos_marked[3][3];

void main()
{
int i,ch,j;
char ans;
/* clrscr();
printf("\n\t\t\t\tTIC TAC TOE");
printf("\n\t\t\t\t");
for(i=1;i<=11;i++)
{
delay(10);
printf("*");
}*/
do
{
clrscr();
printf("\n\t\t\t\tTIC TAC TOE");
printf("\n\t\t\t\t");
for(i=1;i<=11;i++)
{
delay(10);
printf("*");
}
printf("\n1.Start The Game");
printf("\n2.Quit The Game");
printf("\nEnter your choice(1-2) : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
chk=0;
win=0;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 1


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
{
pos_for_X[i][j]=0;
pos_for_O[i][j]=0;
pos_marked[i][j]=0;
}
}
printf("\n\n");
clrscr();
printf("\nEnter the name of the player playing
for \'X\': ");
fflush(stdin);
gets(name_X);
printf("\nEnter the name of the player playing
for \'O\': ");
fflush(stdin);
gets(name_O);
Board();
for(;;)
{
if(win==1)
break;
check();
if(chk==9)
{
printf("\n\t\t\tMATCH DRAWS!!");
printf("\nPress any key....");
break;
}
else
chk=0;
printf("\nTURN FOR %s:",name_X);
PlayerX();
do
{
if(wrong_X!=1)
break;
wrong_X=0;
printf("\nTURN FOR %s:",name_X);
PlayerX();
}while(wrong_X==1);
check();
if(chk==9)
{
printf("\n\t\t\tMATCH DRAWS");
printf("\nPress any key....");
break;
}
else
chk=0;
printf("\nTURN FOR %s:",name_O);
PlayerO();
do

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 2


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
{
if(wrong_O!=1)
break;
wrong_O=0;
printf("\nTURN FOR %s:",name_O);
PlayerO();
}while(wrong_O==1);

}
Board();
if(win!=1)
{
printf("\n\t\t\tMATCH DRAWS!!");
printf("\nPress any key.......");
}
getch();
break;
case 2:
printf("\n\n\n\t\t\tThank You For Playing The Game.");
printf("\n\t\t\t###############################");
getch();
exit(1);
break;
}
printf("\nWant To Play(Y/N) ? ");
fflush(stdin);
scanf("%c",&ans);
}while(ans=='y' || ans=='Y');
}

void Board()
{
int i,j;
clrscr();
printf("\n\t\t\t\tTIC TAC TOE BOARD");
printf("\n\t\t\t\t*****************");
printf("\n\n\n");
printf("\n\t\t\t 1\t 2\t 3");
for(i=1;i<=3;i++)
{
printf("\n \t\t\t _____________________________");
printf("\n \t\t\t?\t ?\t ?\t ?");
printf("\n\t\t%d\t",i);
for(j=1;j<=3;j++)
{

if(pos_for_X[i][j]==1)
{
printf(" X");
printf(" ");
}

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 3


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
else if(pos_for_O[i][j]==1)
{
printf(" O");
printf(" ");
}
else
{
printf(" ");
continue;
}
}
printf("\n\t\t\t?\t ?\t ?\t ?");
}
printf("\n\t\t\t------------------------------");
Player_win();
}
void PlayerX()
{
int row,col;
if(win==1)
return;
printf("\nEnter the row no. : ");
fflush(stdin);
scanf("%d",&row);
printf("Enter the column no. : ");
fflush(stdin);
scanf("%d",&col);
if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 || col>3)
{
printf("\nWRONG POSITION!! Press any key.....");
wrong_X=1;
getch();
Board();
}
else
{
pos_for_X[row][col]=1;
pos_marked[row][col]=1;
Board();
}
}
void PlayerO()
{
int row,col;
if(win==1)
return;
printf("\nEnter the row no. : ");
scanf("%d",&row);
printf("Enter the column no. : ");
scanf("%d",&col);
if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 || col>3)
{

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 4


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
printf("\nWRONG POSITION!! Press any key....");
wrong_O=1;
getch();
Board();
}
else
{
pos_for_O[row][col]=1;
pos_marked[row][col]=1;
Board();
}
}
void Player_win()
{
int i;
for(i=1;i<=3;i++)
{
if(pos_for_X[i][1]==1 && pos_for_X[i][2]==1 && pos_for_X[i]
[3]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_X);
printf("\nPress any key............");
return;
}
}
for(i=1;i<=3;i++)
{
if(pos_for_X[1][i]==1 && pos_for_X[2][i]==1 && pos_for_X[3]
[i]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_X);
printf("\nPress any key............");
return;
}
}
if(pos_for_X[1][1]==1 && pos_for_X[2][2]==1 && pos_for_X[3][3]==1)
{
win=1;
printf("\n\nRESULTL: %s wins!!",name_X);
printf("\nPress any key......");
return;
}
else if(pos_for_X[1][3]==1 && pos_for_X[2][2]==1 &&
pos_for_X[3][1]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_X);
printf("\nPress any key.....");
return;
}

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 5


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003

for(i=1;i<=3;i++)
{
if(pos_for_O[i][1]==1 && pos_for_O[i][2]==1 && pos_for_O[i]
[3]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
}
for(i=1;i<=3;i++)
{
if(pos_for_O[1][i]==1 && pos_for_O[2][i]==1 && pos_for_O[3]
[i]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
}
if(pos_for_O[1][1]==1 && pos_for_O[2][2]==1 && pos_for_O[3][3]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
else if(pos_for_O[1][3]==1 && pos_for_O[2][2]==1 &&
pos_for_O[3][1]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
}
void check()
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
if(pos_marked[i][j]==1)
chk++;
else
continue;
}}}

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 6


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
OUTPUT

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 7


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
Practical 2
#include<stdio.h>
#include<conio.h>

struct node
{
int x, y;
struct node *next;

}*root, *left, *right;

void BFS(int reqJug1, int reqJug2)


{
struct node *temp1 = left, *temp2 = right;
printf("\nBFS Result\n");
printf("(%d , %d)\n", root->x, root->y);
delay(1000);
while(1)
{
printf("(%d , %d)\n", temp1->x, temp1->y);
delay(1000);
if((temp1->x == reqJug1)&&(temp1->y == reqJug2))
break;
temp1 = temp1->next;
printf("(%d , %d)\n", temp2->x, temp2->y);
delay(1000);
if((temp2->x == reqJug1)&&(temp2->y == reqJug2))
break;
temp2 = temp2->next;
}
}
int isNodePresent(struct node *nextState, int maxJug1, int maxJug2,
int reqJug1, int reqJug2)
{
struct node *temp;
if((nextState->x == reqJug1) && (nextState->y == reqJug2))
return(0);
if((nextState->x == maxJug1) && (nextState->y == maxJug2))
return(1);
if((nextState->x == 0) && (nextState->y == 0))
return(1);
temp = left;
while(1)
{
if((temp->x == nextState->x) && (temp->y == nextState->y))
return(1);
else if(temp->next == NULL)
break;
else
temp = temp->next;

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 8


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
}
temp = right;
while(1)
{
if((temp->x == nextState->x) && (temp->y == nextState->y))
return(1);
else if(temp->next == NULL)
break;
temp = temp->next;
}
return(0);
}
struct node* genNewState(struct node *crntState, int maxJug1, int
maxJug2, int reqJug1, int reqJug2)
{
int d;
struct node *nextState;
nextState = (struct node*)malloc(sizeof(struct node));
nextState->x = maxJug1;
nextState->y = crntState->y;
if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1, reqJug2) != 1)
return(nextState);
nextState->x = crntState->x;
nextState->y = maxJug2;
if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1, reqJug2) != 1)
return(nextState);
nextState->x = 0;
nextState->y = crntState->y;
if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1, reqJug2) != 1)
return(nextState);
nextState->y = 0;
nextState->x = crntState->x;
if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1, reqJug2) != 1)
return(nextState);
if((crntState->y < maxJug2) && (crntState->x != 0))
{
d = maxJug2 - crntState->y;
if(d >= crntState->x)
{
nextState->x = 0;
nextState->y = crntState->y + crntState->x;
}
else
{
nextState->x = crntState->x - d;
nextState->y = crntState->y + d;
}
if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1,
reqJug2) != 1)
return(nextState);
}
if((crntState->x < maxJug1) && (crntState->y != 0))

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 9


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
{
d = maxJug1 - crntState->x;
if(d >= crntState->y)
{
nextState->y = 0;
nextState->x = crntState->x + crntState->y;
}
else
{
nextState->y = crntState->y - d;
nextState->x = crntState->x + d;
}
if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1,
reqJug2) != 1)
return(nextState);
}
return(NULL);
}

void generateTree(int maxJug1, int maxJug2, int reqJug1, int reqJug2)


{
int flag1, flag2;
struct node *tempLeft, *tempRight;
root = (struct node*)malloc(sizeof(struct node));
root->x = 0; root->y = 0; root->next = NULL;
left = (struct node*)malloc(sizeof(struct node));
left->x = 0; left->y = maxJug2; left->next = NULL;
right = (struct node*)malloc(sizeof(struct node));
right->x = maxJug1; right->y = 0; right->next = NULL;
tempLeft = left;
tempRight = right;
while(1)
{
flag1 = 0; flag2 = 0;
if((tempLeft->x != reqJug1) || (tempLeft->y != reqJug2))
{
tempLeft->next = genNewState(tempLeft, maxJug1, maxJug2,
reqJug1, reqJug2);
tempLeft = tempLeft->next;
tempLeft->next = NULL;
flag1 = 1;
}
if((tempRight->x != reqJug1) || (tempRight->y != reqJug2))
{
tempRight->next = genNewState(tempRight, maxJug1, maxJug2,
reqJug1, reqJug2);
tempRight = tempRight->next;
tempRight->next = NULL;
flag2 = 1;
}
if((flag1 == 0) && (flag2 == 0))
break;

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 10


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
}
}

void main()
{
int maxJug1, maxJug2, reqJug1, reqJug2;
clrscr();
printf("Enter the maximum capacity of jug1 :: ");
scanf("%d",&maxJug1);
printf("Enter the maximum capacity of jug2 :: ");
scanf("%d",&maxJug2);
printf("Enter the required water in jug1 :: ");
scanf("%d",&reqJug1);
printf("Enter the required water in jug2 :: ");
scanf("%d",&reqJug2);
generateTree(maxJug1, maxJug2, reqJug1, reqJug2);

BFS(reqJug1, reqJug2);
getch();
}

OUTPUT

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 11


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003

Practical 3
#include<stdio.h>
#include<conio.h>

struct node
{
int x, y;
struct node *next;

}*root, *left, *right;


void DFS()
{
struct node *temp;
temp = left;
printf("DFS Result\nStart State :: (%d , %d)\n", root->x, root->y);
printf("Possible DFS Result \n");
while(1)
{
printf("(%d , %d)\n", temp->x, temp->y);
delay(1000);
if(temp->next == NULL)
break;
temp = temp->next;
}
temp = right;
printf("Possible DFS Result 2\n");
while(1)
{
printf("(%d , %d)\n", temp->x, temp->y);
delay(1000);
if(temp->next == NULL)
break;
temp = temp->next;
}
}

int isNodePresent(struct node *nextState, int maxJug1, int maxJug2,


int reqJug1, int reqJug2)
{
struct node *temp;
if((nextState->x == reqJug1) && (nextState->y == reqJug2))
return(0);
if((nextState->x == maxJug1) && (nextState->y == maxJug2))
return(1);
if((nextState->x == 0) && (nextState->y == 0))
return(1);
temp = left;
while(1)

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 12


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
{
if((temp->x == nextState->x) && (temp->y == nextState->y))
return(1);
else if(temp->next == NULL)
break;
else
temp = temp->next;
}
temp = right;
while(1)
{
if((temp->x == nextState->x) && (temp->y == nextState->y))
return(1);
else if(temp->next == NULL)
break;
temp = temp->next;
}
return(0);
}
struct node* genNewState(struct node *crntState, int maxJug1, int
maxJug2, int reqJug1, int reqJug2)
{
int d;
struct node *nextState;
nextState = (struct node*)malloc(sizeof(struct node));
nextState->x = maxJug1;
nextState->y = crntState->y;
if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1, reqJug2) != 1)
return(nextState);
nextState->x = crntState->x;
nextState->y = maxJug2;
if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1, reqJug2) != 1)
return(nextState);
nextState->x = 0;
nextState->y = crntState->y;
if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1, reqJug2) != 1)
return(nextState);
nextState->y = 0;
nextState->x = crntState->x;
if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1, reqJug2) != 1)
return(nextState);
if((crntState->y < maxJug2) && (crntState->x != 0))
{
d = maxJug2 - crntState->y;
if(d >= crntState->x)
{
nextState->x = 0;
nextState->y = crntState->y + crntState->x;
}
else
{
nextState->x = crntState->x - d;

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 13


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
nextState->y = crntState->y + d;
}
if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1, reqJug2) !=
1)
return(nextState);
}
if((crntState->x < maxJug1) && (crntState->y != 0))
{
d = maxJug1 - crntState->x;
if(d >= crntState->y)
{
nextState->y = 0;
nextState->x = crntState->x + crntState->y;
}
else
{
nextState->y = crntState->y - d;
nextState->x = crntState->x + d;
}
if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1, reqJug2) != 1)
return(nextState);
}
return(NULL);
}

void generateTree(int maxJug1, int maxJug2, int reqJug1, int reqJug2)


{
int flag1, flag2;
struct node *tempLeft, *tempRight;
root = (struct node*)malloc(sizeof(struct node));
root->x = 0; root->y = 0; root->next = NULL;
left = (struct node*)malloc(sizeof(struct node));
left->x = 0; left->y = maxJug2; left->next = NULL;
right = (struct node*)malloc(sizeof(struct node));
right->x = maxJug1; right->y = 0; right->next = NULL;
tempLeft = left;
tempRight = right;
while(1)
{
flag1 = 0; flag2 = 0;
if((tempLeft->x != reqJug1) || (tempLeft->y != reqJug2))
{
tempLeft->next = genNewState(tempLeft, maxJug1, maxJug2,
reqJug1, reqJug2);
tempLeft = tempLeft->next;
tempLeft->next = NULL;
flag1 = 1;
}
if((tempRight->x != reqJug1) || (tempRight->y != reqJug2))
{
tempRight->next = genNewState(tempRight, maxJug1, maxJug2,
reqJug1, reqJug2);

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 14


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
tempRight = tempRight->next;
tempRight->next = NULL;
flag2 = 1;
}
if((flag1 == 0) && (flag2 == 0))
break;
}
}

void main()
{
int maxJug1, maxJug2, reqJug1, reqJug2;
clrscr();
printf("Enter the maximum capacity of jug1 :: ");
scanf("%d",&maxJug1);
printf("Enter the maximum capacity of jug2 :: ");
scanf("%d",&maxJug2);
printf("Enter the required water in jug1 :: ");
scanf("%d",&reqJug1);
printf("Enter the required water in jug2 :: ");
scanf("%d",&reqJug2);
generateTree(maxJug1, maxJug2, reqJug1, reqJug2);

DFS(reqJug1, reqJug2);
getch();
}

OUTPUT

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 15


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003

Practical 4
#include <stdio.h>
#include<conio.h>

void towers(int, char, char, char);

int main()

int num;
clrscr();

printf("Enter the number of disks : ");

scanf("%d", &num);

printf("The sequence of moves involved in the Tower of Hanoi are


:\n");

towers(num, 'A', 'C', 'B');


getch();
return 0;

void towers(int num, char frompeg, char topeg, char auxpeg)

if (num == 1)

printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);

return;

towers(num - 1, frompeg, auxpeg, topeg);

printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 16


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
towers(num - 1, auxpeg, topeg, frompeg);
}

OUTPUT

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 17


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003

Practical 5

#include <stdio.h>
#include<conio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
clrscr();
printf("Enter number of elements\n");
scanf("%d",&n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d",&array[c]);

printf("Enter value to find\n");


scanf("%d", &search);

first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last) {


if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Not found! %d is not present in the list.\n", search);
getch();
return 0;
}

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 18


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003

OUTPUT

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 19


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
Practical 6
Practical 6.1
predicates
likes(symbol,symbol)
hobby(symbol, symbol)
plays(symbol,symbol)

clauses
likes(megha,wine).
likes(ashish,wine).
likes(chaitali,coke).
likes(maya,coke).
likes(ravi,wine).

hobby(megha,cricket).
hobby(ravi,chess).
hobby(chaitali,chess).
hobby(maya,cricket).
hobby(ravi,cricket).

plays(megha,piano).
plays(maya,violin).
plays(maya,piano).
plays(ravi,piano).

OUTPUT

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 20


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003

Practical 6.2
predicates
parent(symbol, symbol)
male(symbol)
female(symbol)
mother(symbol, symbol)
son(symbol, symbol)
daughter(symbol, symbol)
grandfather(symbol, symbol)
grandmother(symbol, symbol)
brother(symbol, symbol)
sister(symbol, symbol)
uncle(symbol, symbol)
aunt(symbol, symbol)
nephew(symbol, symbol)
niece(symbol, symbol)
cousines(symbol, symbol)

clauses

male(jonathan).
male(john).
male(kelly).
male(arnold).
male(louise).

female(jenifer).
female(marry).
female(alies).
female(martha).
female(nency).

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 21


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
parent(jonathan,john).
parent(jenifer,john).
parent(jonathan,arnold).
parent(jenifer,arnold).
parent(jonathan,martha).
parent(jenifer,martha).
parent(john,kelly).
parent(marry,kelly).
parent(john,alies).
parent(marry,alies).
parent(arnold,nency).
parent(arnold,louise).

mother(M,C):-
clearwindow(),
female(M),
parent(M,C),
write(M," is a Mother of ",C),nl.

son(S,P):-
clearwindow(),
male(S),
parent(P,S),
write(S," is a son of ",P),nl.

daughter(D,P):-
clearwindow(),
female(D),
parent(P,D),
write(D," is a daughter of ",P),nl.

grandfather(GF,C):-
clearwindow(),

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 22


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
male(GF),
parent(GF,M),
parent(M,C),
write(GF," is a GrandFather of ",C),nl.

grandmother(GM,C):-
clearwindow(),
female(GM),
parent(GM,M),
parent(M,C),
write(GM," is a GrandMother of ",C),nl.

brother(B,S):-
male(B),
parent(P,B),
parent(P,S),
write(B," is a brother of ",S),nl.

sister(S,B):-
female(S),
parent(P,S),
parent(P,B),
write(S," is a sister of ",B),nl.

uncle(U,C):-
clearwindow(),
brother(U,P),
parent(P,C),
write(U," is a uncle of ",C),nl.

aunt(A,C):-
clearwindow(),
sister(A,P),

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 23


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
parent(P,C),
write(A," is a aunt of ",C),nl.

nephew(C,P):-
clearwindow(),
male(C),
brother(P,A),
parent(A,C),
write(C," is a nephew of ",P),nl.

nephew(C,P):-
clearwindow(),
male(C),
sister(P,A),
parent(A,C),
write(C," is a nephew of ",P),nl.

niece(C,P):-
clearwindow(),
female(C),
brother(P,A),
parent(A,C),
write(C," is a niece of ",P),nl.

niece(C,P):-
clearwindow(),
female(C),
sister(P,A),
parent(A,C),
write(C," is a niece of ",P),nl.

cousines(C1,C2):-
clearwindow(),

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 24


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
parent(A,C1),
parent(B,C2),
brother(A,B),
write(C1," is a cousine of ",C2),nl.

cousines(C1,C2):-
clearwindow(),
parent(A,C1),
parent(B,C2),
sister(A,B),
write(C1," is a cousine of ",C2),nl.
OUTPUT

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 25


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003

Practical 6.3
domains
disease, indication=symbol

predicates
symptom (disease, indication)

clauses
symptom(chicken_pox,high_fever).
symptom(chicken_pox,chills).
symptom(flu,chills).
symptom(cold,runny_nose).
symptom(flu,runny_nose).

OUTPUT

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 26


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
Practical 7
Practical 7.1
domains

list=integer*

predicates

findsum(list)
sum(list,integer)

clauses

findsum(L):-
sum(L,Sum),
write("\nSum Of Given List : ",Sum).

sum([],0).

sum([X|Tail],Sum):-
sum(Tail,Temp),
Sum=Temp+X.
OUTPUT

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 27


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
Practical 7.2
predicates
star(integer).
count(integer,integer).
clauses
star(1):-write('*'),!.
star(X):-X<=0,!.
star(X):-count(1,X),Z=x-1,nl,star(Z),!.
count(X,Y):-X<=Y,write('*'),X1=X+1,count(X1,Y),!.
count(X<Y):-X>Y,!.

OUTPUT

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 28


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
Practical 7.3

predicates
go

fact(integer, integer)
clauses

go:-

write (“Enter Number:”),


readint(N),

fact(N,A),
write (A).

fact(1,A):-
A=1.
fact(N,A):-

B=N-1,

Fact(B,S),

A= N*S.
OUTPUT

Practical 8

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 29


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
Practical 8.1

predicates go
check(integer)
clauses
go:-
write("Enter the Year : "),
readint(Year),
check(Year).
check(Y):-
Z=0,
A=Y mod 1000,
B=A mod 100,
Z=B mod 4,
write("Year ",Y," is a Leap Year.").
check(Y):-
write("Year ",Y," is not a leap year.").
OUTPUT

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 30


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
Practical 8.2

predicates
go
buzz(integer)
check(integer)
buzz3(integer)
buzz7(integer)
clauses
go:-
buzz(1).
buzz(26).
buzz(N):-
write(N),
check(N),
NN=N+1,
buzz(NN).
check(N):-
R=N mod 3,
buzz3(R),
P=N mod 7,
buzz7(P),
write("\n").
buzz3(0):-
write(" Buzz3").
buzz3(X).
buzz7(0):-
write(" Buzz7").
buzz7(X).

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 31


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
OUTPUT

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 32


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
Practical 9
predicates fibonacci(integer,integer,integer) go
clauses go:-
clearwindow(),
write("Enter length of series : "), readint(LEN),
nl, fibonacci(LEN,1,0). fibonacci(0,_,_).
fibonacci(LEN,P,Q):-
LEN>0,
L=LEN-1,
VAL=P+Q,
write(VAL),nl,
PP=P,
P1=VAL,
Q1=PP,
fibonacci(L,P1,Q1).
goal
clearwindow(),
go.
OUTPUT

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 33


AIRTIFICIAL INTELLIGENCE(2180703) ENROLL: 140513107003
Practical 10

John gives Marry a book.

John gave Marry the book yesterday.

ALPHA COLLEGE OF ENGINEERING & TECHNOLOGY 34

You might also like