package experiment;
import javax.swing.JOptionPane;
public class Covert {
public static void main(String[] args){
String numberString = JOptionPane.showInputDialog("Please enter a number!");
int number = Integer.parseInt(numberString);
covertNumber(number);
}
private static void covertNumber(int number) {
// if the number is a positive integer
if ( number >= 0){
int i = 0;
int sum = 0;
do{
sum += ((number % 2)* (Math.pow(10, i)));
number = (int) number/2;
i++;
}while(number != 0);
for(int j = 0; j < (32 - i); j++){
System.out.print(0);
}
System.out.print(sum);
}
// if the number is negative
else {
number = number * (-1);
int i = 0;
int[] num = new int[32];
for(i = 0; i < 32; i ++){
num[i] = coverting(number % 2);
number = (int) (number /2);
}
if(num[0] == 0)
num[0] = 1;
else{
int k = 0;
do{
num[k] = coverting(num[k]);
k++;
}while(num[k] == 1);
num[k] = 1;
}
for(int j = 31; j >= 0 ; j--){
System.out.print(num[j]);
}
}
}
private static int coverting(int num){
if(num == 1)
num = 0;
else
num = 1;
return num;
}
}