-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecToBin.java
More file actions
25 lines (25 loc) · 877 Bytes
/
DecToBin.java
File metadata and controls
25 lines (25 loc) · 877 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
import java.util.Scanner;
public class DecToBin{
public static void main(String[] args) throws InterruptedException {
Scanner read = new Scanner(System.in) ;
System.out.print("Enter your decimal number : ");
int a = read.nextInt(); int rest, result ;
if (a==0)System.out.println("In decimal is : 0") ; else if
(a==1) System.out.println("In decimal is : 1");else {
int bits = (int) (Math.floor(Math.log(a)/Math.log(2))+1);
int [] b = new int [bits] ; int h = bits ;
do {
rest = a%2 ; result = a/2 ;
b[h-1]=rest;
h-- ;
a = result ;
}while(result >= 2 );
b[0]=result;
System.out.print("In binary is : ");
for (int i = 0 ; i< bits ; i++ ) {
System.out.print(b[i]);
}
System.out.println();
}
}
}