Can anyone help me with this problem. My output matches the one in the question when I run the program in Codeblocks but I get WA on submitting here.Here is my code:
#include<stdio.h>
#include<math.h>
#include<string.h>
/*converting the decimal number into binary number it will be shown in the output */
int decimaltobinary(int decimal)
{
int binary,rem=0,i=1;
while(decimal!=0)
{
binary=decimal%2;
rem+=binary*i;
decimal=decimal/2;
i*=10;
}
return rem;
}
/converting the given binary number into decimal number/
int binarytodecimal(int n)
{
int dec=0,i1=0;
while(n!=0)
{
int rem=n%10;
dec+=rem*pow(2,i1);
i1++;
n=n/10;
}
return dec;
}
int main()
{
int test,n1,n2;
char operation[5];
scanf("%d", &test);
/*converting the given binary number=test case into decimal number=test case in order to run the test cases*/
test=binarytodecimal(test);
while(test--)
{
scanf("%s %d %d", operation, &n1, &n2);
/*if n1 is less than n2 then it will increase the value of n1 should be replaced by A + 2 to the power N, where N is the smallest integer such that 2 to the power N > max{A,B}*/
int i=0; /*this initialization will be used for counting the digits of the smaller number*/
if(n1<n2 && strcmp(operation,"10")==0)
{
int n=n1; /*here n1 is stored in n for counting the digits of the smaller number, n is used so that the given number is not changed*/
while(n!=0)
{
int rem=n%10;
i++;
n=n/10;
}
}
/*converting the given binary number into decimal number*/
n1=binarytodecimal(n1);
n2=binarytodecimal(n2);
/*increasing the value of the smaller number if the operation is subtraction*/
if(n2>n1 && strcmp(operation,"10")==0)
{
n1=n1+pow(2,i);
}
/*from here to the end of the code all the needed operations will be performed*/
/*comparing the two numbers*/
if(strcmp(operation,"0")==0)
{
if(n1>n2) /*prints 1*/
{
int i=0;
printf("1\n");
}
else /*prints 0*/
{
printf("0\n");
}
}/*comparison ended*/
/*addition*/
else if (strcmp(operation,"1")==0) /*prints n1+n2*/
{
printf("%d\n", decimaltobinary(n1+n2));
}/*addition ended*/
/*subtraction*/
else if (strcmp(operation,"10")==0) /*prints n1-n2*/
{
printf("%d\n", decimaltobinary(n1-n2));
} /*subtraction ended*/
/*multiplication*/
else if (strcmp(operation,"11")==0) /*prints n1*n2*/
{
printf("%d\n", decimaltobinary(n1*n2));
} /*multiplication ended*/
/*division*/
else if (strcmp(operation,"100")==0) /*prints n1/n2*/
{
if( n2==0)
{
printf("0 0\n");
}
else
{
printf("%d %d\n", decimaltobinary(n1/n2), decimaltobinary(n1%n2));
}
}/*division ended*/
} /*end of test case*/
return 0;
}