160 Programming and Data Structures
{
int :c[5 ]b{ 1 , 1 , 1 ,1 ,1 };
in t y a x [ 0 ] , i ;
c l r s c r ();
printf ("n Values in different Iterations. n");
printf ("====== == ========= ===========
">;
for (t=0;i<4;f++)
{
y=y*2+x[i+l];
printf ("[%d] %d ",i+l,y);
I
printf ("nEquivalent o f[ ");
fo r (I=0;i<5;f++)
printf ("%d ",x[i]);
printf (" ] in Decimal Number is : ");
printf ("%dt",y);
getcheO;
)
OUTPUT;
Values in different Iterations.
[113 [2] 7 [3] 15 [4] 31
Equivalent of
[11111 ] in Decimal Number is : 31
Explanation A binary number is initialized in an array. The first element of an array is assigned to
the variable ' y '. The logic used here is to multiply it by 2 to the variable 'y' and add the next
successive bit. For this the equation y=y* 2 +x [ i+1 ] is used. The number of iterations to be carried
out is equal to the number of binary bits minus one. Here five bits are initialized, hence the number
of iterations will be four.
6.46 Write a program to add a parity bit with four binary bits such that the total number of
one's should be even.
# include <stdio.h>
# include <conio.h>
void m a i n ()
{
int b i t [5],j ,c= 0;
clrscr();
printf (J'n Enter four bits :");