Binary to decimal in java


 import java.util.*;  
 public class PracticeDemo {  
      public static void main(String [] args){  
           Scanner sc=new Scanner(System.in);  
           int binaryInput=sc.nextInt();  
           int remainder=0,i=0,value=0,decimal=0,input=binaryInput;  
           do{  
                remainder=input%10;  
                if(remainder==1){  
                     decimal+=(int)Math.pow((int)2,(int) i);  
                }  
                else if(remainder==0){  
                     i=i;}  
                value=input/10;  
                input=value;  
                i++;  
           }while(value>=10);  
           int output=0;  
           if(value==1){  
                output=decimal+(int)Math.pow((int)2, (int)i);  
           System.out.println(output);       
           }  
           else{  
                output=decimal;  
                System.out.println(output);  
           }  
           sc.close();  
      }  
      }  

Output:011
3

Comments

Popular posts from this blog

Reasoning-Number Series

Reasoning-Letter Series

Profit and Loss