0% found this document useful (0 votes)
26 views5 pages

A C Program To Check Parity of Number

Uploaded by

ramji patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

A C Program To Check Parity of Number

Uploaded by

ramji patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

A C Program To Check Parity Of Number


#include<stdio.h>
int main()
{
int parity=0,num;
printf("enter the number");
scanf("%d",&num);
while(num) //LOOP WILL OPERATE UNTIL NUMBER BECOMES 0..BY RIGHT
SHIFTING AGAIN & AGAIN
{
if(num&1) //....MAKE (num & 1)..&..extract 1 bit from 8 bit number..if that 1bit is 1
then ...parity changes...else if that particular bit is 0 found...then parity did not changes...
parity=!parity;
num=num>>1; //after checking 1st bit...check the next bit to that bit which is already
checked...in the same 8 bit number...by shifting 1 right ..//
}
if(parity == 1) //1 is representing that parity is odd..//
printf("parity is odd");
else
printf("parity is even");//0 is representing that parity is even..//
return 0;
}
2. C program to implement Checksum

#include<stdio.h>
#include<math.h>

int sender(int arr[], int n) {


int checksum, sum = 0, i;
printf("\n****SENDER SIDE****\n");
for(i = 0; i < n; i++)
sum += arr[i];

printf("SUM IS: %d", sum);


checksum = ~sum; // 1's complement of sum
printf("\nCHECKSUM IS: %d", checksum);
return checksum;
}

void receiver(int arr[], int n, int sch) {


int checksum, sum = 0, i;
printf("\n\n****RECEIVER SIDE****\n");
for(i = 0; i < n; i++)
sum += arr[i];

printf("SUM IS: %d", sum);


sum = sum + sch;
checksum = ~sum; // 1's complement of sum
printf("\nCHECKSUM IS: %d", checksum);
}

int main() {
int n, sch;
printf("\nENTER SIZE OF THE STRING: ");
scanf("%d", &n);

int arr[10]; // You can set this to a fixed size, e.g., 10

if(n > 10) {


printf("Error: Size exceeds limit.\n");
return -1;
}

printf("ENTER THE ELEMENTS OF THE ARRAY TO CALCULATE


CHECKSUM:\n");
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

sch = sender(arr, n);


receiver(arr, n, sch);

return 0;
}

3. C program to implement CRC

#include <stdio.h>

#include <conio.h>

#include <string.h>

void main()

int i,j,keylen,msglen;

char input[100], key[30],temp[30],quot[100],rem[30],key1[30];

clrscr();

printf("Enter Data: ");

gets(input);

printf("Enter Key: ");

gets(key);

keylen=strlen(key);

msglen=strlen(input);

strcpy(key1,key);

for (i=0;i<keylen-1;i++)

input[msglen+i]='0';

for (i=0;i<keylen;i++)
temp[i]=input[i];

for (i=0;i<msglen;i++) {

quot[i]=temp[0];

if(quot[i]=='0')

for (j=0;j<keylen;j++)

key[j]='0'; else

for (j=0;j<keylen;j++)

key[j]=key1[j];

for (j=keylen-1;j>0;j--)

if(temp[j]==key[j])

rem[j-1]='0'; else

rem[j-1]='1';

rem[keylen-1]=input[i+keylen];

strcpy(temp,rem);

strcpy(rem,temp);

printf("\nQuotient is ");

for (i=0;i<msglen;i++)

printf("%c",quot[i]);

printf("\nRemainder is ");

for (i=0;i<keylen-1;i++)

printf("%c",rem[i]);

printf("\nFinal data is: ");


for (i=0;i<msglen;i++)

printf("%c",input[i]);

for (i=0;i<keylen-1;i++)

printf("%c",rem[i]);

getch();

You might also like