63. PHP Program to find out the Sum of Even and Product of Odd digits of given 6 digit Number.
How does this program work?
- This program is used to find out the Sum of Even and Product of Odd digits in the given 6 digit Number using PHP.
- In this program odd and even A number is even if it is perfectly divisible by 2.
- When the number is divided by 2, we use the remainder operator % to compute the remainder.
- If the remainder is zero, the number is even then sum will be executes else the number is odd then product will be executed by using given logic.
Here is the code
<html>
<head>
<title>PHP Program To find the Sum of even and Product of odd digits of given 6 digit number</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num1" value="" placeholder="Enter number " maxlength="6"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$n = $_POST['num1'];
$n1 = $n;
$n2 = $n;
echo "Given 6 digit number : ".$n2;
$sum = 0;
while ($n != 0)
{
if($n%2==0) // The digit is even then sum will be executes
$sum = $sum + $n % 10;
$n = $n/10;
}
echo "Sum of even digits of given 6 digits number is: ".("$sum")." ";
$product = 1;
while ($n1 != 0)
{
if($n1%2==1) // The digit is odd then product will be executes
$product = $product * ( $n1 % 10);
$n1 = $n1/10;
}
echo "Product of odd digits of given 6 digits number is: ". ("$product");
return 0;
}
?>
</body>
</html>