Archive

Posts Tagged ‘Binary to Decimal’

Java Program to convert binary to decimal – Q17

January 9, 2009 Leave a comment

Q17: Java Program to convert binary to decimal

import javax.swing.*;

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

… from College notes (BCA/MCA assignments):