Summing a number’s digits **programming exercise**.

*Pythia, Oracle of Delphi, wants to do some numerology. She promises to Pray for Apollos favor to this who:*

Write a function which takes a number as input and returns the **sum **the **absolute value **of its **digits**.

Š•xample: Input: 12 -> 1+2 = 3 output: 3 52 -> 5+2 = 7 output: 7-78 ->7+8 = 15 output: 15

Scroll down to see the solution of this math…I mean coding problem.

public static int SumDigits(int n) {//first we need to get theabsolute valueof the number //theAbs()inMathinc#does exactly that. //not that we cant use a simple IF but //Math.Abs()is more readablen = Math.Abs(n);//Letsget the digitsof the number //and in the same timesumthemint sum = 0;//lets see how to get the last digit of the number 123? //well will use the reminder operator //123%10 =3//and then the second one ? //123 / 10 =12 //12%2 =2//and so one until we haveno digits left.while (n != 0) { sum += n % 10; n /= 10; } return sum;`/*`

`* If you are not familiar with this small`

algorithmfor taking`* the`

digits of a number, experiment a little with it.`* Make a small function that takes any`

integerand return`* a`

char arraywith itsdigits. Also its good for showing off in`* job interviews ;)`

`*/`

}