Numbers represented in this system are commonly called binary numbers. Because of its straightforward implementation in digital electronic circuitry using logic gates, the binary system is used internally by almost all modern computers and computer-based devices such as mobile phones.

Steps to convert a decimal number to Binary number:

1. Take modulas(%) of decimal number by 2.

2. Now divide decimal number by 2.

3. Repeat these steps until condition matches.

4. Now reverse that number.

Go through the code given below.

`<?php`

$numberToConvert = 7;

if($numberToConvert > 0){

$tempArray = array();

echo "Binary of $numberToConvert is : ";

while($numberToConvert){

$tempArray[] = $numberToConvert%2;

$numberToConvert = (int)($numberToConvert/2);

}

for($i = count($tempArray); $i >=0; $i--){

echo $tempArray[$i];

}

}else{

echo 'Number should be greater then zero.';

}

?>

## No comments:

## Post a Comment