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 :");
Loop Control Statements 161
fo r (j=0;j<4;j++)
{
scanf (“% d",&cbi t[j]);
if(bit[j]==l)
C + + ;
else
if(bit[j]>l I I bit[j]<0)
(
H
continue;
I
if (c%2~0)
bit[j]-0;
else
bit[j]=l;
printf ("n Message bits together with parity bit:");
fo r (j=0;j<5;j++)
printf ("%d",bit[jl);
I
Enter four bits : 1111
Message bits together with parity b it : 11110
Explanation In the above program the four binary bits are entered through the keyboard. If the
number of one's entered is eveft, then the fifth bit is set to zero otherwise one. This is checked by the
i f statement.
6.47 Write a program to convert binary to decimal number. Enter the binary bits by using for
loop.
#inclu d e < std io . h>
#inclu d e <conio.h>
#inclu d e < p ro cess.h >
void main()
{
int a^/ZflO] , i ;
clrscr();
printf("nEnter the number of bits:-");
scanf( "%d"Ab);
162 Programming and Data Structures
printf(“ Enter the binary bits:-");
for(i=0,'i<b,'i++)
scanf ("%d",&z[i] );
a=z[0];
for(i=0,-i<(b-l),'i++)
I
a=a*2+z[i+l];
printf("n%d",a);
I
printf(J' Decimal Number is : %d",a);
getcheO;
I
OUTPUT;
Enter the number of bits:- 5
Enter the binary bits:- 1 0 0 01
17
Decimal Number is : 17
Explanation In the above program a number of binary bits is entered. Using this number individual
bits are entered through the keyboard. For this f o r loop is used. The first element of an array is
assigned to variable ' a ' . The logic used here is to multiply by 2 to the variable 'a ' and add the
next successive bit. For this the equation a=a*2+z [ 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.
Following is the Table of logic gates AND, OR and EX-OR. Write programs for verification of
truth Tables.
Table 6.5 Logic gates of AND, OR and EX-OR
Inputs
Outputs
And Gate
Or Gate Ex-Or Gate
A B
C C C
0
0
0 0 0
0
1
0 1 1
1
0 0 1 1
1
1
1 1 0
Loop Control Statements 163
6.48 Write a program to verify the truth table of AND GATE. Assume AND GATE has two
input bits A & B and one output bit C.
# include <stdio.h>
# include <conio.h>
void mainO
{
int x,a (4 ] ,b[4] ,c [4 ];
c lrs c r O j
printf ("Enter Four B its:");
fo r (x=0pc<4pc++)
I
scanf ("%d"&a[x]);
if(a[x]>l 11 a[x]<0)
{
r.-i
continue;
)
)
printf ("Enter Four B its:);
fo r (x=0pr<4pc++)
I
scanf ("%d"&blxl);
if (b[x]>l 11 b[x]<0)
{
x - ;
continue;
I
I
printf ("nAB C");
for (x=0;x<4;x++)
(
if (a[x]--l && b[xj==sl)
cfx j-l;
else
c[x]=0;
printf (%d %d %d",a[xj,b[x],c[xj);
I
164 Programming and Data Structures
OUTPUT;
Enter Four Bits:
1 0
Enter Four Bits: 1 0
A B C
1 1 1
0 0 0
1 0 0
0 1 0
Explanation The four binary bits for A and B are entered. The i f statement checks the bits of A
and B. If both of them are one then the output C must be 1, otherwise for all other condition output
is 0.
6.49 Write a program to verify the truth table of OR GATE.
# include <stdio.h>
# include <conio.h>
void main()
{
int x ,a [4 ] ,b [ 4 ] ,c [4 ];
c l r s c r ();
printf ("Enter Four Bits
fo r (x=0;x<4;x++)
I
scanf ("%d",&a[x]);
if (a[x]>l I I a[x]<0)
I
x -;
continue;
I
I
printf ("Enter Four Bits :");
for (x=0;x<4;x++)
I
scanf ("%d"Ablx]);
if (b[x]>l I I b[xj<0)
{
^ r
continue;
i
i
for (x=0;x<4;x++)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.227.4