Arrays 231
Explanation In the above program two fo r loops are used. The inner fo r loop generates four binary
bits for each hexadecimal number and assigns it to array b in [ ] [ ]. The 0 to F hexadecimal numbers
are taken from the outer fo r loop. If the bit is 1 counter % c ' is increased. The value of c is checked for
even or odd condition. If c is odd the parity b it% 0' is displayed otherwise ' 1' .The output is displayed.
7.36 Write a progarm to convert binary to gray codes.
# include <stdio.h>
# include <conio.h>
void mpinQ
{
int bin[16] [4] ,x,y,asO,ceO;
clrscrO ;
for (x*0;x<16/x++)
{
y*x;
for (a=ยป0;a<4;a++)
{
bln[x][a]=y%2;
y-y/2;
}
}
c lrs c rO ;
printf (" n Binary Bits Gray B its");
printf C n ========== ===========");
fo r (x~0;x<16;x++)
I
printf (" n ");
for(y=3,-y>=0,-y~)
printf ("% 3d",bin[x]ly]);
printf ("% 5 d %2d %2d %2d",bin[x][3], bin [x][3]*bin[x][2 ],bin[x][2 ]Ab in [x ][l],b in [x ][l]Abm [x][0 ]);
OUTPUT:
Binary Bits Gray Bits
0000 0000
0001 0001
0010 0011
0011 0010
0100 0110
0101 0111
0110 0101