80. PHP Program to Convert Binary value into Decimal number.
How does this program work?
- This program is used to convert Binary value into equivalent of Decimal number using PHP.
- That means convert number with base value 2 to base value 10.
- The integer entered by user will store in one variable. Divide that number by 10.
- Store the remainder and that will be multiplied by base value, Declare base value and initialize with 1 means (2^0).
Here is the code
<html>
<head>
<title>PHP Program To convert Binary value into Decimal number</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num" value="" placeholder="Enter any binary number"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$n = $_POST['num'];
$n1 = $n;
$decimal_value = 0;
$base = 1;
while ($n1 > 0)
{
$rem = $n1 % 10;
$decimal_value = $decimal_value + $rem * $base;
$n1 = $n1 / 10 ;
$base = $base * 2;
}
echo "The given Binary number is: ".$n;
echo "The Equivalent decimal number is: ".$decimal_value;
return 0;
}
?>
</body>
</html>