Write a c program to simulate even parity generator and checker.
Here below is the C program to simulate even parity generator and checker.
#include <stdio.h> #include <string.h> // Function to calculate the parity bit for even parity char generateEvenParity(char data[]) { int onesCount = 0; // Count the number of 1's in the data for (int i = 0; i < strlen(data); i++) { if (data[i] == '1') { onesCount++; } } // If the number of 1's is odd, add 1 as parity bit // If the number of 1's is even, add 0 as parity bit if (onesCount % 2 == 0) { return '0'; // Even parity: no need to add 1 } else { return '1'; // Odd parity: need to add 1 } } // Function to check if the received data has even parity int checkEvenParity(char data[]) { int onesCount = 0; // Count the number of 1's in the data for (int i = 0; i < strlen(data); i++) { if (data[i] == '1') { onesCount++; } } // If the number of 1's is even, it's valid; otherwise, it's not return (onesCount % 2 == 0) ? 1 : 0; // 1 means valid, 0 means invalid } int main() { char data[100]; char parityBit; // Input the binary data printf("Enter the binary data (without parity bit): "); scanf("%s", data); // Generate even parity bit parityBit = generateEvenParity(data); printf("Generated Even Parity Bit: %c\n", parityBit); // Display data with the parity bit printf("Data with Parity Bit: %s%c\n", data, parityBit); // Check the even parity of the received data with the parity bit printf("Enter received data (with parity bit): "); scanf("%s", data); if (checkEvenParity(data)) { printf("The received data has valid even parity.\n"); } else { printf("The received data has invalid parity.\n"); } return 0; }