-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecimalToBinary_1.java
More file actions
39 lines (31 loc) · 918 Bytes
/
decimalToBinary_1.java
File metadata and controls
39 lines (31 loc) · 918 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
28
29
30
31
32
33
34
35
36
37
38
39
/*
# convert a decimal input to binary
steps:
-> converts using successive division method
-> a while loop, in each steps, gets remainder = decimal%2
-> adds the remainder in front of a resultBin string
-> makes decimal /= 2, until decimal value becomes zero
-> after the loop, resultBin string is converted to long & returned
*/
import java.util.*;
public class Main
{
public static long makebin (int decimal)
{
int remainder = 0;
String resultBin = "";
while (decimal != 0)
{
remainder = decimal%2;
resultBin = remainder + resultBin;
decimal /= 2;
}
return Long.parseLong (resultBin);
}
public static void main (String [] args)
{
Scanner sc = new Scanner (System.in);
int decimal = sc.nextInt();
System.out.println (makebin (decimal));
}
}