mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
Added Decimal to BInary Conversion
This commit is contained in:
12
Conversions/DecimalToBinary.js
Normal file
12
Conversions/DecimalToBinary.js
Normal file
@ -0,0 +1,12 @@
|
||||
function decimalToBinary(num) {
|
||||
var bin = [];
|
||||
while (num > 0) {
|
||||
bin.unshift(num % 2);
|
||||
num >>= 1; // basically /= 2 without remainder if any
|
||||
}
|
||||
console.log("The decimal in binary is " + bin.join(''));
|
||||
}
|
||||
|
||||
decimalToBinary(2);
|
||||
decimalToBinary(7);
|
||||
decimalToBinary(35);
|
Reference in New Issue
Block a user