Skip to main content

Binary Converter - Netbeans Java Codes


You can also download text file of this code here:  BinaryConverter.txt

Java Code:

public class BinaryConv {
public static void main(String[] args) {
  Scanner in = new Scanner(System.in);
  int decimalNumber;
  String binaryNumber;
  System.out.print("Enter a positive integer: ");
  decimalNumber = in.nextInt();
  if (decimalNumber <= 0)
   System.out.println("ERROR: entered integer is nonpositive.");
  else {
   binaryNumber = "";
// algorithm step by step
// initial: binaryNumber = "", decimalNumber = 123
// step 1 : binaryNumber = "1 ", decimalNumber = 61
// step 2 : binaryNumber = "11 ", decimalNumber = 30
// step 3 : binaryNumber = "011 ", decimalNumber = 15
// step 4 : binaryNumber = "1011 ", decimalNumber = 7
// step 5 : binaryNumber = "1 1011 ", decimalNumber = 3
// step 6 : binaryNumber = "11 1011 ", decimalNumber = 1
// step 6 : binaryNumber = "111 1011 ", decimalNumber = 0
// stop   : (decimalNumber != 0)
   while (decimalNumber != 0) {
// add spaces to separate 4-digit groups 
    if (binaryNumber.length() % 5 == 0)
     binaryNumber = " " + binaryNumber;
// extract last digit in binary representation
// and add it to binaryNumber [Find More Java Codes at www.sumikuma.tk]
    binaryNumber = (decimalNumber % 2) + binaryNumber;
// cut last digit in binary representation
    decimalNumber /= 2;
   }
   System.out.println("Binary: " + binaryNumber);
  }
 }
}

[Find More Java Codes at www.sumikuma.tk]

Comments

Popular posts from this blog

Captcha - Netbeans Java Codes

You can also download text file of this code here:   Captcha.txt Java Code: private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // [Find More Java Codes at www.sumikuma.tk] if ("Cleven Eleven".equals(txt1.getText())) JOptionPane.showMessageDialog(this,"You answer is correct"); else JOptionPane.showMessageDialog(null, "You are unsuccessful, Please Try Again"); } How To Display Image? : jLabel1>Properties>Icon>(Under External Image) Import to Image>Select Suitable Image from File Chooser>Select Target Folder>Click Ok> Close the Properties Window. [Find More Java Codes at www.sumikuma.tk]

Tic Tac Toe - Netbeans Java Codes

Java Code: public class TicTacToe implements ActionListener { final String VERSION = "1.0"; //Setting up ALL the variables[Find More Java Codes at www.sumikuma.tk] JFrame window = new JFrame("Tic-Tac-Toe " + VERSION); JMenuBar mnuMain = new JMenuBar(); JMenuItem mnuNewGame = new JMenuItem("New Game"), mnuInstruction = new JMenuItem("Instructions"), mnuExit = new JMenuItem("Exit"), mnuAbout = new JMenuItem("About"); JButton btn1v1 = new JButton("Player vs Player"), btn1vCPU = new JButton("Player vs CPU"), btnBack = new JButton("<--back"); JButton btnEmpty[] = new JButton[10]; JPanel pnlNewGame = new JPanel(), pnlNorth = new JPanel(), pnlSouth = new JPanel(), pnlTop = new JPanel(), pnlBottom = new JPanel(), pnlPlayingField = new JPanel(); JLabel lblTitle = new JLabel("Tic-Tac-Toe"); JTextArea txtMessage = new JTextArea(); final int winCombo[][] = new int[][] { {1, 2, 3}, {...

Get Pascal's Triangle - Netbeans Java Codes

Run-Shot of Pascal's Triangle Java Code: public class Pascalstriangle { public static void main(String args[]) { int x = 6; int triangle[][] = new int[x][x]; for (int i = 0; i < x; i++) { for (int j = 0; j < x; j++) { triangle[i][j] = 0; } } for(int i = 0; i < x; i++) { triangle[i][0] = 1 ; } for (int i = 1; i < x; i++) { for (int j = 1; j < x; j++) { triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j]; } } for (int i = 0; i < x; i++) { for(int j=0;j<=i;j++) { System.out.print(triangle[i][j]+ " "); } System.out.println(); } } } [Find more Netbeans Java Code at www.sumikuma.tk] You can also download text file version of this code: Pascal-triangle_javacode.txt