Coding Practice

Write a program that choose Even parity / Odd parity according to user choose. And accordingly performs Even parity / Odd parity process

Parity Bit Checker

Example 1:
Enter 1 for Even parity and 2 for Odd parity.
Input: 1

Even parity:
Input:
Data: 110110
Output:
modified data: 1101100
Parity bit: 0

Example 2:
Enter 1 for Even parity and 2 for Odd parity.
Input: 2

Odd parity:
Input:
Data: 1101101
Output:
Modified data: 11011010
Parity bit: 0

Sample Output
Enter 1 for Even parity and 2 for Odd parity.

Enter your choice: 1

Even parity:
Data: 110110

Modified Data: 1101100
Parity Bit: 0
C-Source Code
#include<stdio.h>
#include<string.h>

void main()
{
    int i, choice, even_parity_len, count = 0, p_bit;
    char parity_bit, even_parity[100];
main:
    printf("Enter 1 for Even parity and 2 for Odd parity.\n\nEnter your choice: ");
    scanf("%d", &choice);

    switch(choice)
    {
    case 1:
        printf("\nEven parity:\nData: ");
        scanf("%s", even_parity);

        for(i = 0; even_parity[i] != '\0'; i++);
        even_parity_len = i;

        for(i = 0; i < even_parity[i]; i++)
        {
            if(even_parity[i] == '1')
            {
                count++;
            }
        }

        if(count%2 == 0)
        {
            even_parity[even_parity_len] = '0';
            p_bit = 0;
        }
        else
        {
            even_parity[even_parity_len] = '1';
            p_bit = 1;
        }
        break;

    case 2:
        printf("\nOdd parity:\nData: ");
        scanf("%s", even_parity);

        for(i = 0; even_parity[i] != '\0'; i++);
        even_parity_len = i;

        for(i = 0; i < even_parity[i]; i++)
        {
            if(even_parity[i] == '1')
            {
                count++;
            }
        }

        if(count%2 == 0)
        {
            even_parity[even_parity_len] = '1';
            p_bit = 1;
        }
        else
        {
            even_parity[even_parity_len] = '0';
            p_bit = 0;
        }
        break;

    default:
        printf("\nInvalid Input! Please enter 1 or 2.\n");
        goto main;
        break;
    }
    even_parity[even_parity_len+1] = '\0';

    printf("\nModified Data: %s\nParity Bit: %d\n\n", even_parity, p_bit);
}
Sample Output
Enter 1 for Even parity and 2 for Odd parity.

Enter your choice: 2

Odd parity:
Data: 1101101

Modified Data: 11011010
Parity Bit: 0

No comments:

Post a Comment

Change Theme
X