Home > Java > Java Program to convert binary to decimal – Q17

Java Program to convert binary to decimal – Q17


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):


Advertisement
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: