Archive

Posts Tagged ‘Decimal to Binary’

Java Program to convert decimal to binary number – Q16

January 8, 2009 Leave a comment

Q16: Java Program to convert decimal to binary number

import javax.swing.*;

class dec_bin{
	public static void main(String args[]){
		String inp, bin;
		int dec, x;
		
		inp = JOptionPane.showInputDialog("Enter a Decimal 
    Number:");
		dec = Integer.parseInt(inp);
		
		bin = "";
		while(dec != 0){
			x = dec % 2;
			bin  = x + bin;
			dec /= 2;
		}
		System.out.println("Binary: " + bin);
	}
}

… from College notes (BCA/MCA assignments):