To convert decimal to binary from 1 to 1024 numbers using if-else

PROGRAM :

public class BinaryC {
        
  
    public static void main(String[] args) {
      
        
         System.out.println("this is program for conversion of decimal to binary");
        System.out.println("enter the number between 1 and 1024");
        Scanner s = new Scanner (System.in);
        int a,b,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,t=0,u=0,v=0,w=0;
        int x=0;
        a=s.nextInt();
        if (a%2==0)
        { b=0;
          c=a/2;}
          else  {b=1;c=a/2;
          }
        
        if( c > 0 && c%2==0)
        { d=0;
          e=c/2;}
          else if(c>0) { d=1;e=c/2;
          }
        
         if( e > 0 && e%2==0)
        { f=0;
          g=e/2;}
          else if(e>0) { f=1;g=e/2;
          }
        
         if( g > 0 && g%2==0)
        { h=0;
          i=g/2;}
          else if(g>0) { h=1;i=g/2;
          }
         if( i > 0 && i%2==0)
        { j=0;
          k=i/2;}
          else if(i>0) { j=1;k=i/2;
          }
         if( k > 0 && k%2==0)
        { l=0;
          m=k/2;}
          else if(k>0) { l=1;m=k/2;
          }
           if( m > 0 && m%2==0)
        { n=0;
          o=m/2;}
          else if(m>0) { n=1;o=m/2;
          }
           if( o > 0 && o%2==0)
        { p=0;
          q=o/2;}
          else if(o>0) { p=1;q=o/2;
          }
           if( q > 0 && q%2==0)
        { r=0;
          t=q/2;}
          else if(q>0) { r=1;t=q/2;
          }
           if( t > 0 && t%2==0)
        { u=0;
          v=t/2;}
          else if(t>0) { u=1;v=t/2;
          }
           if( v > 0 && v%2==0)
        { w=0;
          x=v/2;}
          else if(v>0) { w=1;x=v/2;
          }
          
        
        System.out.println("the binary is :"+w +u +r +p +n +l +j +h +f +d +b);
}
}
        

OUTPUT :

this is program for conversion of decimal to binary
enter the number between 1 and 1024
5
the binary is :00000000101

        
        

No comments:

Post a Comment