1

I am a newbie to programming .here I have been solving a simple problem in functional programming (OZ) which is finding the sum of the Digits of a 6 digit positive integer. Example:- if n = 123456 then output = 1+2+3+4+5+6 which is 21. here I found a solution like below

fun {SumDigits6 N}
{SumDigits (N Div 1000) + SumDigits (N mod 1000)}
end

and it says the argument (N Div 1000) gives first 3 digits and the argument (N mod 1000) gives us last 3 digits. and yes I getting the correct solution but my Doubt is how could they giving correct solutions. I mean in given example isn't (N Div 1000) of 123456 gives 123 right not 1+2+3 and similarly (N mod 1000) of123456 gives us 456 not 4+5+6 right ?. in that case, the answer should be 123+456 which is equals to 579 not 21 right ? what Iam missing here.I apologize for asking such simple question but any help would be appreciated. Thank you :)

læran91
  • 283
  • 1
  • 15

4 Answers4

1

You are missing the most important thing here. It is supposed to happen in a loop and each time the value of N changes.

For example in the first iteration the Div gives 1 and mod gives 6 so you add 1 and 6 and store the result and the number is also modified (it becomes 2345) in the second iteration the div gives 2 and mod gives 5 you add 2+5+previous result and the number is also modified.. This goes on till the number becomes zero

Saram Ali Azhar
  • 234
  • 1
  • 18
1

Your function is recursive, so every time the number get smaller, untill is just 0, then it goes back summing all the partial result. You can do it with an accumulator to store the result, in this simple way:

declare
fun {SumDigit N Accumulator}
 if N==0 then Accumulator
 else {SumDigit (N div 10) Accumulator+(N mod 10)}
 end
end

{Browse {SumDigit 123456 0}}
rok
  • 2,574
  • 3
  • 23
  • 44
0

i think the most elegant way is the function --

static int SumOfDigit(int n)
 {
if (n < 10) return n;
return SumOfDigit(SumOfDigit(n/10)+n%10);

}

simple and true :-)

  • That does not seem to work: SumOfDigit(19) = SumOfDigit(SumOfDigit(1) + 9) = SumOfDigit(1 + 9) = SumOfDigit(10) = SumOfDigit(SumOfDigit(1) + 0) = SumOfDigit(1 + 0) = SumOfDigit(1) = 1 – Gabriel Devillers Nov 27 '18 at 22:39
0
int main()
{
  int n,m,d,s=0;
  scanf("%d",&n);
  m=n;
  while(m!=0)
 {
       d=m%10;
       s=s+d;
       m=m/10;
 }
printf("Sum of digits of %d is %d",n,s);
} 
  • 4
    While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Mickael B. May 07 '20 at 03:00
  • While this code may solve the OP's issue, it is better to add context, or an explanation as to why this solves the OP's problem, so future visitors can learn from your post, and apply this knowledge to their own issues. High Quality Answers are much more likely to be upvoted, and contribute to the value and quality of SO as a platform. You can also add a link to documentation, if that helps. – SherylHohman May 07 '20 at 17:52