![]() |
Given a Decimal Number, and the task is to convert a given Decimal Number to a Binary Number using PHP. Examples: Input: 10 There are different methods to convert the Decimal to a Binary number, these are: Table of Content Using decbin() FunctionThe decbin() function converts the given decimal number to the binary number. It returns a string containing a binary representation of the given decimal number argument. decbin stands for decimal to binary. Syntax: string decbin( $value )
Example: PHP
Output
Binary Number: 101010 Using Conversion AlgorithmWe will use the conversion algorithm to convert the Decimal Number to its equivalent Binary Number in PHP. We will define a custom function decimalToBinary that takes a decimal number as input and iteratively divides it by 2, collecting the remainders at each step. Concatenate these remainders in reverse order to get the Binary number. The loop continues until the decimal number becomes zero. Example: PHP
Output
Binary Number: 11001 Using RecursionWe will use Recursive function to convert Decimal number to Binary Number in PHP. First, we declare a recursive function decimalToBinary that checks if the input number is zero, it returns ‘0’. Otherwise, it makes a recursive call with the integer division of the number by 2 and appends the remainder to the result. PHP
Output
Binary Number: 11001 |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |