What's new

PHP Pa help po Decimal to Bin, Oct, Hex

kiplog

Grasshopper
Joined
Oct 25, 2021
Posts
13
Reaction
0
Points
15
Problem: Create a simple web application that converts decimal into binary, octal, hexadecimal using functions and arrays.

Pa help naman po pano ayusin yung code ko, yun sanang mag iinput ng decimal tsaka lang lalabas yung answer sa binary, octal, hexadecimal.

PHP:
<!DOCTYPE html>
<html>
<body>

<?php
function decToBin($n)
{
 $binary;
 $i = 0;
 while ($n != 0)
 {
  $binary[$i] = $n % 2;
  $n = (int)($n / 2);
  $i++;
 }
 for ( $j = $i - 1; $j >= 0; $j--)
  echo $binary[$j];
}
$n = 33;
 decToBin($n);
?>

<?php
function decToOctal($n)
{
 $octal;
 $i = 0;
 while ($n != 0)
 {
  $octal[$i] = $n % 8;
  $n = (int)($n / 8);
  $i++;
 }
 for ( $j = $i - 1; $j >= 0; $j--)
  echo $octal[$j];
}
$n = 33;
decToOctal($n);
?>

<?php
function decToHex($n)
{
 $hexadecimal;
 $i = 0;
 while ($n != 0)
 {
  $hexadecimal[$i] = $n % 16;
  $n = (int)($n / 16);
  $i++;
 }
 for ( $j = $i - 1; $j >= 0; $j--)
  echo $hexadecimal[$j];
}
$n = 33;
decToHex($n);
?>

</body>
</html>
 

Similar threads

Back
Top