PHP code to find the sum of digits of a number using for loop

This program finds the sum of the digits of the given number using for loop.


Code


<html>
<head>
<title>SUM OF THE DIGITS</title>
</head>
<body>
<font size=5>SUM OF THE DIGITS</font>
<br/><br/>
<form action=sum-of-digits.php method="post">
ENTER NUMBER:<input type="text" name="t1">
<br><br>
<input type="submit" value="SUM">
<?php
if(isset($_POST["t1"]))
{
$n=$_POST["t1"];
$sum = 0;
for(;$n>0;$n=floor($n/10))
{
$b=$n%10;
$sum=$sum+$b;
}
echo "sum of digit is:$sum";
}
?>
</form>
</body>
</html>

Output



sum of digits of a number in php.
Sum of the digits of a number

No comments: