JAVA
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class DecimalToBinary
{
/* A binary number is made up of elements called bits where each bit can be 1 or 0 */
public static void main(String[] args)
{
int number = 156;
String binary = toBinary(number);
System.out.println(binary);
}
public static String toBinary(int n)
{
if (n == 0)
{
return "0";
}
String binary = "";
while (n > 0)
{
// The % (modulus) operator returns the remainder of the division
int rem = n % 2;
// add the result (0 or 1)
binary = rem + binary;
// divide the number by 2
n = n / 2;
Enter to Rename, Shift+Enter to Preview
OUTPUT
Ejecutar