logo CodeStepByStep logo

sum_of_digits

Language/Type: PHP loops parameters return

Write a function named sum_of_digits that accepts an integer parameter and computes and returns the sum of all the digits of that number.

For example, sum_of_digits(38015) should return 17 (since 3 + 8 + 1 + 0 + 5 = 17). For negative numbers, return the same value as if the number were positive. For example, sum_of_digits(-72) returns 9 (since 7 + 2 = 9).

You may assume that the value passed is an Integer type.

Function: Write a PHP function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.