Java Program to Convert Binary number to Decimal number

In general, Number systems can be convertible. Following Simple java program will demonstrate how to convert the Binary number system into a Decimal number system.

BinaryToDecimal.java

public class BinaryToDecimal 
{
	public static int getDecimal(int binary)
	{  
	    int decimal = 0;  
	    int n = 0;  
	    while(true){  
	      if(binary == 0)
	      {  
	        break;  
	      }
	      else 
	      {  
	          int temp = binary%10;  
	          decimal += temp*Math.pow(2, n);  
	          binary = binary/10;  
	          n++;  
	       }  
	    }  
	    return decimal;  
	}  
	
	public static void main(String args[])
	{    
	System.out.println("Decimal of 111 is: "+getDecimal(111));  
	 
	}
}

Output:

Decimal of 111 is: 7

Leave a Reply

Your email address will not be published. Required fields are marked *