Binary To Decimal with while loop - Java Algorithm

Java examples for Algorithm:Number

Description

Binary To Decimal with while loop

Demo Code


public class Main {
  public static void main(String args[]) throws Exception {
    int mBinary = 111;
    int dec = 0;/*from w ww . ja va 2  s.  c  o m*/
    int power = 0;
    while (mBinary > 0) {
      int temp = mBinary % 10;
      dec += temp * Math.pow(2, power);
      mBinary = mBinary / 10;
      power++;

    }
    System.out.print("Decimal : " + dec);
  }
}

Related Tutorials