Created
January 13, 2017 14:37
-
-
Save M-ZubairAhmed/faf276583b4a11a3c5045d7656329726 to your computer and use it in GitHub Desktop.
Conversion of a Binary Number to Decimal Number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int binaryToDecimal (int binaryNumber){ | |
int digitIndex; | |
int decimalNum = 0; | |
int powerIndex = 0; | |
while(( binaryNumber != 0) && ( binaryNumber >0)){ | |
digitIndex = binaryNumber % 10; | |
decimalNum = decimalNum + (int)(Math.pow(2,powerIndex) * digitIndex); | |
binaryNumber = binaryNumber / 10; | |
powerIndex++; | |
} | |
return decimalNum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment