PHP Program to convert decimal value to binary value


February 15, 2022, Learn eTutorial
1375

How to convert the decimal value into binary value?

To convert a decimal value into a binary value we need to divide the decimal number by 2 and get an integer quotient and again divide the remainder by 2 and get the quotient and repeat this process until the quotient becomes 0. And after that, we have to print the quotient in reverse order. For example, if the decimal number is 10

10 / 2 = 5  quotient 0

5 / 2 = 2   quotient 1

2 / 2 = 1   quotient 0

1 / 2 = 0   quotient 1

The binary equivalent of 10 will be 1010

How to convert the decimal value into binary value using PHP?

In this program, we are accepting the decimal value from the user and assigning it to the variable num and we have to call the user-defined function decToBin() with argument num. In the function decToBin() we first Create an empty array binNum[] and then assign the value 0 into the variable i. After that, we perform while loop until the condition 'n > 0' becomes false to get store the quotient values into the array binNum[] for that we first assign the computed value of 'n % 2' into the array binNum[i] then assign the computed integer value of 'n / 2' into the variable n and at last increment the value of the variable i after the completion of the while loop we perform the for loop to print the elements of an array in reverse order. First, assign the value 'i - 1' to the variable j and perform the loop until the condition 'j >= 0' becomes false and increment the value of the variable j in every iteration and in the block of the loop we print the value of the array binNum[i].

ALGORITHM

Step 1: Accept the decimal value into the variable num

Step 2: Call the user-defined function decToBin() with argument num

ALGORITHM function: dectToBin(n)

Step 1: Create an empty array binNum[]

Step 2: Assign the value 0 into the variable i

Step 3: Perform the sub-steps until the condition 'n > 0' becomes false

        (i) Assign the computed value of 'n % 2' into the array binNum[i]

        (ii) Assign the computed integer value of 'n / 2' into the variable n

        (iii) Increment the value of the variable i

Step 4: Perform the for loop to print the elements of the array in reverse order. First, assign the value 'i - 1' to the variable j and perform the sub-step until the condition 'j >= 0' becomes false and increment the value of the variable j in every iteration

        (i) Print the value of the array binNum[i]

PHP Source Code

                                          <?php
function decToBin($n)
{
    $binNum = array();
    $i = 0;
    while ($n > 0) {
        $binNum[$i] = $n % 2;
        $n = (int)($n / 2);
        $i++;
    }
    for ($j = $i - 1; $j >= 0; $j--)
        echo $binNum[$j];
}
$num = readline("Enter the value in decimal: ");
echo "Binary equivalent of $num is ";
decToBin($num);
?>
                                      

OUTPUT

Enter the value in decimal: 28
Binary equivalent of 28 is 11100