Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

Prolog problem to add two numbers

Posted By: Milind Mishra     Category: Artificial Intelligence     Views: 69701

Prolog problem to add two numbers.

?- start.
X= |: 1.
Y= |: 2.
Sum is 3
yes

Code for Prolog problem to add two numbers in Artificial Intelligence

predicates
    start.
    sum.    
clauses
          
        start:- sum,nl.
        
        sum:-     write("X= "),readreal(X),
            write("Y= "),readreal(Y),
            S = X+Y,
            write("Sum is "),write(S).

goal
    start.
  
Share: 


Didn't find what you were looking for? Find more on Prolog problem to add two numbers Or get search suggestion and latest updates.

Milind Mishra
Milind Mishra author of Prolog problem to add two numbers is from India.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Nitin Tripathi from United States Comment on: May 23
Hello Milind,
I am trying to learn PROLOG, and saw this code.

But in my case,
I can see the following output.
can you please elaborate the behavior.

[trace] [4] 257 ?- start.
Call: (30) start ? creep
Call: (31) sum ? creep
Exit: (31) sum ? creep
Call: (31) nl ? creep

Exit: (31) nl ? creep
Exit: (30) start ? creep
true .


While my code is as follow.

%predicates
start.
sum.
%clauses

start :- sum, nl.

sum :-
write("X = "),
readreal(X),
write("Y = "),
readreal(Y),
S = X + Y,
write("SUM is = "),
write(S).

View All Comments