To find sum of digits of a number just add all the digits.
12345= 1 + 2 + 3 + 4 + 5
12345= 15
Take the number.
Divide the number by 10.
Add the remainder to a variable.
Repeat the process until remainder is 0.
Given program shows the sum of digits of 12345.
<?php
$num = 12345;
$sum=0; $rem=0;
for ($i =0; $i<=strlen($num);$i++)
{
$rem=$num%10;
$sum = $sum + $rem;
$num=$num/10;
}
echo "Sum of digits 12345 is $sum";
?>
Output: Sum of digits 12345 is 15
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning