Parity Bit encoder

#include <stdio.h>
#include <string.h>

int main() {
    char binary[100], parity;
    int count = 0;

    // Input binary number and parity type
    printf("Enter a binary number: ");
    scanf("%s", binary);
    printf("Choose parity - even (e) or odd (o): ");
    scanf(" %c", &parity);

    // Count the number of 1's
    for (int i = 0; binary[i]; i++)
        count += (binary[i] == '1');

    // Append parity bit
    strcat(binary, ((parity == 'e') == (count % 2 == 0)) ? "0" : "1");

    printf("Parity encoded data: %s\n", binary);
    return 0;
}

Comments