81. PHP Program to Convert Decimal number into Octal number.
How does this program work?
- This program is used to convert given Decimal number into equivalent of Octal number using PHP.
- That means convert number with base value 10 to base value 8.
- The integer entered by user will store in one variable. Divide that number by 8.
- Store the remainder when the number is divided by 8 in an array, Repeat the above step until the number is greater than zero.
Here is the code
<html>
<head>
<title>PHP Program To convert Decimal number into Octal number</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num" value="" placeholder="Enter any integer"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$n = $_POST['num'];
$num = $n;
$octal;
$i = 1;
while($n>0)
{
$octal[$i] = $n % 8;
$n = (int)($n/8);
$i++; //Count will increment
}
echo "The Decimal number is: ".$num." ";
echo "The Equivalent octal number is : ";
for($j= $i-1; $j > 0; $j--) //To print octal number
echo $octal[$j];
return 0;
}
?>
</body>
</html>