Decimal to Binary
Convert a decimal number to its binary representation.
Sample input
13
Sample output
1101
Solution
n = 13
print(bin(n)[2:])
const n = 13;
console.log(n.toString(2));
public class Main {
public static void main(String[] args) {
int n = 13;
System.out.println(Integer.toBinaryString(n));
}
}
fun main() {
val n = 13
println(Integer.toBinaryString(n))
}
let n = 13
print(String(n, radix: 2))
void main() {
int n = 13;
print(n.toRadixString(2));
}
#include <iostream>
#include <string>
using namespace std;
int main() {
int n = 13;
string binary = "";
while (n > 0) {
binary = char('0' + n % 2) + binary;
n /= 2;
}
if (binary == "") binary = "0";
cout << binary << endl;
return 0;
}
#include <stdio.h>
int main() {
int n = 13;
char binary[33];
int i = 0;
if (n == 0) binary[i++] = '0';
while (n > 0) {
binary[i++] = '0' + n % 2;
n /= 2;
}
for (int j = i - 1; j >= 0; j--) putchar(binary[j]);
putchar('\n');
return 0;
}