0 Replies - 335 Views - Last Post: 29 March 2009 - 08:48 PM

#1 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

sum of digits of a number (in PROLOG)

Posted 29 March 2009 - 08:48 PM

Description: a simple snippet to find the sum of digits of a given number in PROLOG
sumofdigits(X, X):-
	X < 10.

sumofdigits(X, Y):-
	X > 9, Y1 is X mod 10, X1 is X // 10, sumofdigits(X1, Z), Y is Y1 + Z. 

Is This A Good Question/Topic? 0
  • +

Page 1 of 1