Jump to content

sum of number


Pawan_Agarwal

Recommended Posts

I want to add the number with this code

 

 

while($i>0)
{
$j=$i%10;
$i=$i/10;
$sum=$sum+$j;
}
 
I have number in $i=12345, now after execution, sum must return the sum of all digits of its number 15
 
Example:
$i=23, sum=5
$i=18, sum=9
$i=19, sum=10
 
As you can see the code must terminate as soon as $i==0, but I am facing infinite loop........thanks
 

 

Link to comment
https://forums.phpfreaks.com/topic/279231-sum-of-number/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.