Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

Prolog program to find sum of all the numbers in a given list

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

Prolog program to find sum of all the numbers in a given list.

Code for Prolog program to find sum of all the numbers in a given list in Artificial Intelligence

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.
        
        
OUT PUT
=======

Goal: findsum([1,2,3,4,5])

Sum Of Given List : 15

Yes

-------------------------------------

Goal: findsum([])

Sum Of Given List : 0

Yes

-------------------------------------

Goal: findsum([1,2,3,4,5,6,7,8,9,10])

Sum Of Given List : 55

Yes
  
Share: 



Milind Mishra
Milind Mishra author of Prolog program to find sum of all the numbers in a given list is from India.
 
View All Articles

 
Please enter your Comment

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

 
No Comment Found, Be the First to post comment!