/* File Name : D2B.java */
// PROGRAM TO CONVERT DECIMAL NUMBER INTO BINARY
import java.io.*;
import java.lang.*;
class D2B
{
public static void main(String []arg)throws IOException
{
int a[]=new int[9];
int b[]=new int[9];
int C[]=new int[9];
int D[]=new int[9];
int w[]=new int[9];
int x,y;
int i=0;
int j,q=0;
int k=0;
int l;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the First Decimal Number");
x=Integer.parseInt(br.readLine()); //READING NUMBER
for(i=0;i<9;i++)
{
a[i]=0;
b[i]=0;
C[i]=0;
D[i]=0;
w[i]=0;
}
i=0;
while(x>0)
{
a[i]=x%2;
x=x/2;
i++;
}
System.out.print("Binary Conversion for Firt Number::");
for(i=i-1,j=0;i>=0;i--,j++)
{
b[j]=a[i];
System.out.print(b[j]);
}
System.out.println();
System.out.println("Enter the Second Decimal Number");
y=Integer.parseInt(br.readLine()); //READING NUMBER
while(y>0)
{
C[k]=y%2;
y=y/2;
k++;
}
System.out.print("Binary Conversion for Firt Number::");
for(k=k-1,l=0;k>=0;k--,l++)
{
D[l]=C[k];
System.out.print(D[l]);
}
System.out.println("\nTHE ADDITION IS :");
for(int g=0;g<9;g++)
{
w[g]=b[g]+D[g]+q;
System.out.println(w[g]);
if(b[g]==0 && D[g] =0 && x=0 )
{
q=0;
w[g]=0;
}
else if(b[g]==1 && D[g] =0 && x=0 || b[g]==0 && D[g] =1 && x=0)
{
q=0;
w[g]=1;
}
else if(b[g]==1 && D[g] =1 && x=0)
{
q=1;
w[g]=0;
}
else if(b[g]==1 && D[g] =1 && x=1)
{
q=1;
w[g]=1;
}
System.out.print(w[g]);
}
System.out.println();
}
}
// PROGRAM TO CONVERT DECIMAL NUMBER INTO BINARY
import java.io.*;
import java.lang.*;
class D2B
{
public static void main(String []arg)throws IOException
{
int a[]=new int[9];
int b[]=new int[9];
int C[]=new int[9];
int D[]=new int[9];
int w[]=new int[9];
int x,y;
int i=0;
int j,q=0;
int k=0;
int l;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the First Decimal Number");
x=Integer.parseInt(br.readLine()); //READING NUMBER
for(i=0;i<9;i++)
{
a[i]=0;
b[i]=0;
C[i]=0;
D[i]=0;
w[i]=0;
}
i=0;
while(x>0)
{
a[i]=x%2;
x=x/2;
i++;
}
System.out.print("Binary Conversion for Firt Number::");
for(i=i-1,j=0;i>=0;i--,j++)
{
b[j]=a[i];
System.out.print(b[j]);
}
System.out.println();
System.out.println("Enter the Second Decimal Number");
y=Integer.parseInt(br.readLine()); //READING NUMBER
while(y>0)
{
C[k]=y%2;
y=y/2;
k++;
}
System.out.print("Binary Conversion for Firt Number::");
for(k=k-1,l=0;k>=0;k--,l++)
{
D[l]=C[k];
System.out.print(D[l]);
}
System.out.println("\nTHE ADDITION IS :");
for(int g=0;g<9;g++)
{
w[g]=b[g]+D[g]+q;
System.out.println(w[g]);
if(b[g]==0 && D[g] =0 && x=0 )
{
q=0;
w[g]=0;
}
else if(b[g]==1 && D[g] =0 && x=0 || b[g]==0 && D[g] =1 && x=0)
{
q=0;
w[g]=1;
}
else if(b[g]==1 && D[g] =1 && x=0)
{
q=1;
w[g]=0;
}
else if(b[g]==1 && D[g] =1 && x=1)
{
q=1;
w[g]=1;
}
System.out.print(w[g]);
}
System.out.println();
}
}
Comments
Post a Comment