Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

binary to decimal

import java.util.Scanner;

public class BinaryToDecimal {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String binary = sc.next();
		// This is one line solution for binary to decimal in java
		System.out.println(Integer.parseInt(binary,2));
		
		//This solution is based on formula for binary to decimal conversion
		int n=0,dec=0;
		for(int i=binary.length()-1;i>=0;i--)
		{
			dec = dec + Integer.parseInt(String.valueOf(binary.charAt(i)))*(int)Math.pow(2,n);
			n++;
		}
		System.out.println(dec);

	}

}
Source by prepinsta.com #
 
PREVIOUS NEXT
Tagged: #binary #decimal
ADD COMMENT
Topic
Name
8+7 =