[Löst] Sum Digits - PHP Task

  

4
Ämnesstart

You will be given a single integer. Your task is to find the sum of its digits.

Examples:

sum digits php task

2 Svar
3

My solution:

<?php
 
$input = readline();
 
$number = 0;
 
for ($i = 0; $i < strlen($input); $i++) {
    $number += $input[$i];
}
 
echo $number;
1

Another solution:

<?php
 
$number = intval(readline());
$sum = 0;
 
while ($number > 0) {
    $sum += $number % 10;
    $number = intval($number / 10);
}
 
echo $sum;
Dela: