4
05/11/2024 3:03 e m
Ämnesstart
You will be given a single integer. Your task is to find the sum of its digits.
Examples:
2 Svar
3
05/11/2024 3:04 e m
My solution:
<?php $input = readline(); $number = 0; for ($i = 0; $i < strlen($input); $i++) { $number += $input[$i]; } echo $number;
1
05/11/2024 3:05 e m
Another solution:
<?php $number = intval(readline()); $sum = 0; while ($number > 0) { $sum += $number % 10; $number = intval($number / 10); } echo $sum;