- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecToBin.java
More file actions
Latest commit
27 lines (23 loc) · 658 Bytes
/
Copy pathDecToBin.java
File metadata and controls
27 lines (23 loc) · 658 Bytes
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
importjava.util.Scanner;
publicclassDecToBin {
publicstaticStringtoBinary(intn) {
if (n == 0) {
return"0";
}
Stringbinary = "";
while (n > 0) {
intrem = n % 2;
binary = rem + binary;
n = n / 2;
}
returnbinary;
}
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
System.out.print("Enter a number: ");
intdecimal = sc.nextInt();
Stringbinary = DecToBin.toBinary(decimal);
System.out.println("The binary representation is " + binary);
sc.close();
}
}