Jump to content

sum of number


Go to solution Solved by kicken,

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.