To convert binary to decimal from 1 to 2048 using if - else only.

PROGRAM :

import java.util.Scanner;
public class BinaryDecimal {
    public static void main(String[] args) {
     
     System.out.println("This is program for binary to decimal");
     int a,b,c,d=0,e,f=0,h=0,g=0,k,kk=0,i=0,j=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,y=0;
        System.out.println("Enter the Binary number");
        Scanner s =new Scanner (System.in);
        a= s.nextInt();
        b= a%10;
        c= a/10;
        if (c>0)
        {
           d= c%10;
           e=c/10;
            if(e>0)
           {       f= e%10;
                    g=e/10;
            }
             if(g>0)
             {
                h= g%10;
               i=g/10;
             }
             if (i>0)
               {
              j=i%10;
              kk=i/10;
                }
              if(kk>0)
               {
              l=kk%10;
            m=kk/10;
                }
              if(m>0)
                 {
              n=m%10;
            o=m/10;
                }
              if(o>0)
               {
                 p=o%10;
              q=o/10;
              }
              if(q>0)
               {
              r=q%10;
              t=q/10;
              }
               if(t>0)
               {
              u=t%10;
              v=t/10;
                }
              if(v>0)
               {
              w=v%10;
            x=v/10;
                }
              if(x>0)
               {
              y=x%10;
                }
            }
                         
        b=b*1;
        d= d*2;
        f= f*4;
        h= h*8;
        j= j*16;
        l=l*32;
        n=n*64;
        p=p*128;
        r=r*256;
        u=u*512;
        w=w*1024;
        y=y*2048;
        k=b+d+f+h+j+l+n+p+r+u+w+y;
        System.out.println("the decimal is :" +k);
          }
}

OUTPUT :

This is program for binary to decimal
Enter the Binary number
101
the decimal is :5

No comments:

Post a Comment