import java.util.*;
class ResDiv
{
static int a[]={0,0,0,0,0},q[]=new int[4],b[]=new int[5],b2c[]=new int[5];
public static void comp()
{
int i=4;
do
{
b2c[i]=b[i];
i--;
}while(b[i+1]!=1);
while(i>=0)
{
b2c[i]=(b[i]+1)%2;
i--;
}
System.out.print("\n\tB's complement:");
for(i=0;i< 5;i++)
System.out.print(b2c[i]);
System.out.print("\n");
}
public static void nonresdiv()
{
shiftleft();
if(a[0]==0)
a_minus_b();
else
a_plus_b();
q[3]=(a[0]+1)%2;
}
public static void shiftleft()
{
int i;
for(i=0;i< 4;i++)
a[i]=a[i+1];
a[4]=q[0];
for(i=0;i< 3;i++)
q[i]=q[i+1];
}
public static void a_minus_b()
{
int i,carry=0,sum=0;
for(i=4;i>=0;i--)
{
sum=(a[i]+b2c[i]+carry);
a[i]=sum%2;
carry=sum/2;
}
}
public static void a_plus_b()
{
int i,carry=0,sum=0;
for(i=4;i>=0;i--)
{
sum=(a[i]+b[i]+carry);
a[i]=sum%2;
carry=sum/2;
}
}
public static void main(String args[])
{
Scanner src=new Scanner(System.in);
int i,j,k;
System.out.print("Enter dividend in binary form\t: ");
for(i=0;i< 4;i++)
q[i]=src.nextInt();;
System.out.print("Enter divisor in binary form\t: ");
for(i=0;i< 5;i++)
b[i]=src.nextInt();
comp();
System.out.print("\n\t[A]\t[M]\n");
for(i=0;i< 4;i++)
{
nonresdiv();
System.out.print("\t");
for(j=0;j< 5;j++)
System.out.print(a[j]);
System.out.print("\t");
for(k=0;k< 4;k++)
System.out.print(q[k]);
System.out.print("\n");
}
if(a[0]==1)
a_plus_b();
System.out.print("\t");
for(j=0;j< 5;j++)
System.out.print(a[j]);
System.out.print("\t");
for(k=0;k< 4;k++)
System.out.print(q[k]);
System.out.print("\n");
System.out.print("\n\tThe Quotient Is\t: ");
for(k=0;k< 4;k++)
System.out.print(q[k]);
System.out.print("\n\tThe Remainder Is\t: ");
for(j=0;j< 5;j++)
System.out.print(a[j]);
}
}

could you pls make a few changes in the code such that it also works for 5 digit dividend
ReplyDeleteplease delete the program. it is too lenghty. i hate the program just like i hate my lipstick.
ReplyDeletewhy is it named rohit's gallery????????????????????????????????
ReplyDeletewhy not virat's gallery!!!!
btw i am dhobi's bhakt
could you pls make a few changes in the code such that it also works for 5 digit dividend
ReplyDeletei just copied the comment posted 7 years ago!!! how do u feel?!?!?!?!?!?!
ReplyDeleter u shreyas iyer's brother?????
ReplyDeletei am his distant sister -_*