A
How to take a binary number as input in C?
The ASCII value of 0 is 48 and that of 1 is 49. Here 0 and 1 are decimal numbers and not binary. There is no direct way of treating a number as binary in C, you have to do it manually.
#include <stdio.h>
int main()
{
char b[100];
int s, t, x, y;
scanf("%[^\t\n]s",b); /*scanf to accept multi-word string*/
if(b[0]=='0')
x=0;
else
x=1;
if(b[1]=='0')
y=0;
else
y=1;
s=x|y;t=x^y;
printf("%c \n%c",s,t);return 0;
}
#include <stdio.h>
int main()
{
char b[100],s,t;
scanf("%[^\t\n]s",b); /*scanf to accept multi-word string*/
for(int temp=0; temp<100; ++temp)
b[temp]-='0';
s=b[0]|b[1];
t=b[0]^b[1];
printf("%c \n%c",s,t);
return 0;
}
#include <stdio.h>
int main()
{
char b[100],s,t;
scanf("%[^\t\n]s",b); /*scanf to accept multi-word string*/
for(int i=0; b[i]!=NULL; ++i)
b[i]-='0';
for(int i=0; ++i<digits.size()-1; ++i)
{
--i;
s=digits[i]|digits[i+1];
t=digits[i]&digits[i+1];
printf("\nOR: %d", s);
printf("\nAND: %d", t);
printf("/n");
}
printf("%c \n%c",s,t);return 0;}
Dude, dont be dumb, at least mention what is the problem occurring. And even if it's not working, the logit of treating the string as binary digits is clearly mentioned in the code. use your brain to correct the code and understand the logic. We are here to suggest and to point the wrong, not to do what's your's work.
char i_number[32];
char o_number[32];
int length, a,b;
printf("Enter the binary number : ");
scanf("%s", i_number);
length = strlen(b_number);
// For AND operation;
for (int i = length-1 ; i>0; i++ )
{
if(i_number[i] == '0')
a = 0;
else
a =1;
if(i_number[i-1] == '0')
b = 0;
else
b = 1;
a = a&b;
o_number[i] = a;
}
if(i_number[0] == '0')
a = 0;
else
a =1;
if(i_number[length-1] == '0')
b = 0;
else
b = 1;
a = a&b;
o_number[i] = a;
for (i = length-1 ; i>0; i--)
{
if(i_number[i] == '0')
a = 0;
else
a =1;
if(i_number[i-1] == '0')
b = 0;
else
b = 1;