def dec2bin_recurs(n: int) -> int:
assert n >= 0 and isinstance(n, int), "Input must be positive integer"
if not n: return n
return int(f'{dec2bin_recurs(n//2)}{n%2}')
# Function to convert decimal number
# to binary using recursion
def DecimalToBinary(num):
if num >= 1:
DecimalToBinary(num // 2)
print(num % 2, end = '')
# Driver Code
if __name__ == '__main__':
# decimal value
dec_val = 24
# Calling function
DecimalToBinary(dec_val)
function convertToBinary(x) {
let bin = 0;
let rem, i = 1;
/*rem is the remaining Number, i is the current step total, bin is the binary answer*/
while (x != 0) {
rem = x % 2;
x = parseInt(x / 2);
bin = bin + rem * i;
i = i * 10;
}
return bin;
}
console.log(convertToBinary(4))
#include <iostream>
#include <stdlib.h>
int main ()
{
int i;
char buffer [33];
printf ("Enter a number: ");
scanf ("%d",&i);
itoa (i,buffer,10);
printf ("decimal: %s
",buffer);
itoa (i,buffer,16);
printf ("hexadecimal: %s
",buffer);
itoa (i,buffer,2);
printf ("binary: %s
",buffer);
return 0;
}
def decimalToBinary(n):
return bin(n).replace("0b", "")
# Driver code
if __name__ == '__main__':
print(decimalToBinary(8))
print(decimalToBinary(18))
print(decimalToBinary(7))
std::string binary = std::bitset<8>(n).to_string();
# Function to print binary number using recursion
def convertToBinary(n):
if n > 1:
convertToBinary(n//2)
print(n % 2,end = '')
# decimal number
dec = 34
convertToBinary(dec)
print()