-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.Parity.c
More file actions
73 lines (61 loc) · 1.69 KB
/
1.Parity.c
File metadata and controls
73 lines (61 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include <string.h>
int main()
{
char s[9], ch;
int count = 0, i;
printf("Enter the 8 bit string: ");
scanf("%s", s);
printf("Enter x for even parity and y for odd parity: ");
scanf(" %c", &ch);
for (i = 0; i < 8; ++i)
if (s[i] == '1')
++count;
if (ch == 'x')
{
if (count % 2 == 0)
puts("Entered string is even parity");
else
{
puts("append 1 to string");
s[8] = '1';
printf("String: %s", s);
}
}
else if (ch == 'y')
{
if (count % 2 != 0)
puts("Entered string is odd parity");
else
{
printf("append 1 to string\n");
s[8] = '1';
printf("String: %s", s);
}
}
return 0;
}
// Input/Output:-
// ------------
// case 1:
// ------
// Enter the 8 bit string: 01100110 /* Input */
// Enter x for even parity and y for odd parity: x /* Input */
// Entered string is even parity
// case 2:
// ------
// Enter the 8 bit string: 00101010 /* Input */
// Enter x for even parity and y for odd parity: x /* Input */
// append 1 to string
// String: 001010101
// case 3:
// ------
// Enter the 8 bit string: 00101100 /* Input */
// Enter x for even parity and y for odd parity: y /* Input */
// Entered string is odd parity
// case 4:
// ------
// Enter the 8 bit string: 01110100 /* Input */
// Enter x for even parity and y for odd parity: y /* Input */
// append 1 to string
// String: 011101001