Reputation: 147
Im doing a php activity, that will compute the sum of given number of integer. Sample Output:
int number = 367;
Final output
16
16 is the total sum of 3+6+7.
Here's my Code:
$number = "367";
$int = 0;
for ($i = 0 ; $i < number.length(); $i++ ){
$char = number.charAt(i);
$int += Integer.parseInt(char1.toString());
}
echo $int;
Still i can't get the right answer anyone can help me? Thank you !
regards jay
Upvotes: 0
Views: 197
Reputation: 16989
I'm not exactly sure what you're doing but it seems like you're mixing PHP and Javascript. If you're looking for a PHP solution it's a lot easier than that. Give this a try:
$number = 367;
echo array_sum(str_split($number));
Upvotes: 1