Binary to decimal conversion program in java. For example 10 is decimal representation of binary number 1010.
import java.util.Scanner; public class Conversion { static int power(int pos,long count) { int Mfactor=1; while(count>0) { Mfactor=Mfactor*pos; count=count-1; } return Mfactor; } public static void main(String[] args) { long Number,Remainder,Decimal=0,count=0; Scanner input=new Scanner(System.in); System.out.print("enter Binary Number:"); Number=input.nextInt(); input.close(); while(Number!=0) { Remainder=Number%10; Decimal=Decimal+Remainder*power(2,count); count=count+1; Number=Number/10; } System.out.println("Decimal representation:"+Decimal); } }
OUTPUT: enter Binary Number:1000 Decimal representation:8 -------------------------- enter Binary Number:100000 Decimal representation:32 --------------------------