Tuesday 29 May 2018

Java Program to convert binary into decimal



Binary Numbers are represented in the form of 0's and 1's.

For example:-10 is 2 in decimal form

Sample Program:-


public class BinaryToDecimal {  
      public static void main(String[] args) {  
           int binary = 1110;  
           int decimal = 0;  
           int power = 0;  
           while (binary > 0) {  
                decimal += (binary % 10) * Math.pow(2, power++);  
                binary = binary / 10;  
           }  
           System.out.println("Decimal Number is:" + decimal);  
      }  
 }  


Output:-


Decimal Number is:14  

Enjoy Coding.


No comments:

Post a Comment