This simple Java Application will convert the given decimal value to its Binary.
Following code is to be executed after the user enters a value and presses 'Convert'.
(Button Action)
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int num = Integer.parseInt(jTextField1.getText());
if(num>=0){
int binary[] = new int[50];
int index = 0;
do{
binary[index++]=num%2;
num = num/2;
}
while(num>0);
StringBuilder builder = new StringBuilder();
for (int i=index-1; i>=0;i--) {
builder.append(binary[i]);//to concatenate the array in reverse
}
String text = builder.toString(); // convert to string
jLabel3.setText(text);
}
}
Comments
Post a Comment