172. PHP Program to find the Sum of digits of a given number.
How does this program work?
- This program is used to find the sum of digits of a given number using PHP.
- The integer entered by the user is stored in variable n.
- Declare variable sum to store digits of sum and initialize it with 0.
- After that by following the given logic we find the sum of digits of a given number.
Here is the code
<html>
<head>
<title>PHP Program To find the Sum of digits of given number</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num" value="" placeholder="Enter a number"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit'])) {
$n = $_POST[ 'num'];
$x = $n;
$r = 0;
$sum = 0;
while($n>1)
{
$r = $n%10;
$sum = $sum+$r;
$n = $n/10;
}
echo "Sum of digits of given number ". $x . " is: " .$sum;
return 0;
}
?>
</body>
</html>

