Loop Control Statements 165
I
if (a[x]==0 && b[x]==0)
c[x]=0;
else
c[x]=l;
printf ("n%d %d %d",a[x],b[x],c[x]);
I
Q U E UT;
Enter Four Bits: 1110
Enter Four Bits: 1 0 0 0
A B C
1 1 1
1 0 1
1 0 1
0 0 0
Explanation The four binary bits for A and B are entered. The i f statement checks the bits of A
and B. If one of them is at logic one then the output C must be 1. When the inputs are zero the output
is also 0.
6.50 Write a program to verify the truth table of EX-OR GATE.
# include <stdio.h>
# include <conio.h>
void mainO
{
in t x,a [4 ] , b [4] ,c [4] ;
clrs c rO ;
printf ("Enter Four Bits
fo r (x=0;x<4;x++)
I
scanf ("%d",ha[x]);
if (a[x]>l I I a[x]<0)
I
x~;
continue;
}
I
166 Programming and Data Structures
printf ("Enter Four Bits
fo r (x=0;x<4;x++)
{
scanf ("%d",&b[x]);
if (b[x]>l 1 1 b[x]<0)
I
x -;
continue;
}
i
printf C A B C );
fo r (x=0pc<4,-x++)
I
if (a[x]==0 && b[x]==l)
c[x]=l;
else
if (a[x]==l && b[x]==0)
c[x]=l;
else
c[x]=0;
printf ("n%d %d %d",a[x],bfx],c[xj);
}
OUTPUT;
Enter Four Bits: 1110
Enter Four Bits: 1 0 0 0
A B C
1 1 0
1 0 1
1 0 1
0 0 0
Explanation The four binary bits for A and B are entered. The i f statement checks the bits of A
and B. If the input bits are dissimilar the output C must be 1. When the inputs are similar the output
is also 0. In the two extreme cases the output is zero.
Loop Control Statements 167
6. 51 Write a program to find the Hamming code for the entered binaiy code. Assume the
binary code of four bits in length. The hamming code should be seven bits.
Up: R.W. Hamming developed error correcting and detecting codes in communicating the
four bits data which is to be transmitted contains additional three check bits. The word that
is to be transmitted, its format, will be D7, D6, D5, P4, D3, P2 & PI. Where D bits are data bits
and the P bits are parity bits. PI is set so that it provides even parity over bits PI, D3, D5 &
D7. P2 is set for even parity over bits P2, D3, D6 & D7. P4 is set for even parity over bit3
P4,D5,D6 & D7
# include <stdio.h>
# include <conio.h>
void main()
{
s t a t ic int c [7 ];
in t x ,b [4];
c l r s c r ();
printf ("n Read the Binary Numbers:");
fo r (x=0;x<4;x++)
scanf (“%d",&cb[x]);
I*piece copy operation */
c[0]=b[0];
c[l]=b[lj;
c[2]=b[2J;
c[4]=b[3J;
printf ("n Before XOR operation: “);
fo r (x=0;x<=6;x++)
printf ("%3d",c[x]);
c[6]= c[0]*c[2]*c[4];
c[5]= c[0]Ac[l]Ac[4J;
c[3]= c[0]*c[l]*c[21;
printf ("n Hamming code after XOR operation:");
for (x=0;x<=6;x++)
printf ("%3d",c[xl);
getcheO;
I
OUTPUT:
Read the Binary Numbers : 1 0 1 0
Before XOR operation : 1 0 1 0 0 0 0
Hamming code after XOR operation : 1 0 1 0 0 1 0
168 Programming and Data Structures
Explanation The Hamming code that contains seven bits is initially assumed as zeros. This is
declared with s ta ti c int c [7]. The four bit data is read through the keyboard using the first
for loop. The data bits are placed at the appropriate positions using piece copy operations. The
three parity bits are evaluated using XOR bit wise operations. Ultimately the seven bits are displayed
using the last for loop.
6.52 Write a program to show the results of students who appear in the final examination.
Assume that the students have to appear in six subjects. The result declared should be
as per the following table.
TOTAL MARKS
RESULT
>=420 Distiction
>=360
First Division
>=240 Second Division
Otherwise
Fail
#include <stdio.h>
#include <conio.h>
#define DISTINCTION 420
#define FIRST 360
#define SECOND 240
void main()
{
in t num b er,i,j, roll_no,m ark s,total;
c l r s c r ( );
printf ("n Enter number o f Students: );
scanf (“%d",&cnumber);
printf C ");
fo r (i=l;i<=number;++i)
I
printf ("Enter Roll Number
scanf (“%d",Szroll_no);
total=0,-printf (" n Enter Marks o f 6 Subjects fo r Roll no %d : ",roll_no);
fo r (j=l;j<=6,j++)
I
scanf ("%d",Szmarks);
total=total+marks;
I
printf ("TOTAL MARKS =%d",total);
if(total>= DISTINCTION)
printf (“ (Distinction) ");
else if (total>=FIRST)
printf (“ (First Division) ");
else if(total>=SECOND)
printf (" (Second Division) ");
..................Content has been hidden....................

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