Checksum Program in C and C++

Here you will get the checksum program in C and C++.

A checksum is a mistake identification strategy in Data Communication. It is utilized

for mistakes which may have been presented during transmission or capacity.

It is generally applied to an establishment record after it is gotten from the download server.

The real methodology which yields the checksum, given

information is known as a checksum capacity or checksum calculation.

Checksum technique can just identify mistakes however can’t right the blunder.

In this technique, a checksum is determined dependent on the given twofold strings

which are sent with the information as repetitive bits. This information + checksum is

gotten at the beneficiary end and the checksum is determined once more if the checksum is

0 it implies no blunder in information got, else there exists some mistake in the got information.

With the end goal of this program, we are discovering checksum for 2 parallel strings.

Checksum Algorithm

Take 2 twofold info strings.

Do their parallel aggregate to discover the checksum which will be sent to the goal or to the beneficiary.

In parallel aggregate there are 6 cases:-

In the event that the two bits are 0 and convey is 0, sum=0 and carry=0

In the event that the two bits are 0 and convey is 1,sum=1 and carry=0

In the event that the two bits are 1 and convey is 0,sum=0 and carry=1

In the event that the two bits are 1 and convey is 1,sum=1 and carry=1

In the event that either bit is 1 and convey is 0,sum=1 and carry=0

In the event that either bit is 1 and convey is 1,sum=0 and carry=1

While doing the option we need to include the parallel strings from furthest right end i.e LSB to MSB.

At the point when twofold entirety is done 1’s supplement of it is taken by turning around 1’s to 0’s and the other way around.

The subsequent 1’s supplement is the Checksum.

Stop.

Checksum Program in C

#include<stdio.h>
#include<string.h>
 
int main()
{
    char a[20],b[20];
    char sum[20],complement[20];
    int i,length;
    
	printf("Enter first binary string\n");
    scanf("%s",&a);
    printf("Enter second binary string\n");
    scanf("%s",&b);
    
    if(strlen(a)==strlen(b)){
		length = strlen(a);
		char carry='0';
        
		for(i=length-1;i>=0;i--)
        {
			if(a[i]=='0' && b[i]=='0' && carry=='0')
            {
                sum[i]='0';
                carry='0';
            }
            else if(a[i]=='0' && b[i]=='0' && carry=='1')
            {
                sum[i]='1';
                carry='0';
 
            }
            else if(a[i]=='0' && b[i]=='1' && carry=='0')
            {
                sum[i]='1';
                carry='0';
 
            }
            else if(a[i]=='0' && b[i]=='1' && carry=='1')
            {
                sum[i]='0';
                carry='1';
 
            }
            else if(a[i]=='1' && b[i]=='0' && carry=='0')
            {
                sum[i]='1';
                carry='0';
 
            }
            else if(a[i]=='1' && b[i]=='0' && carry=='1')
            {
                sum[i]='0';
                carry='1';
 
            }
            else if(a[i]=='1' && b[i]=='1' && carry=='0')
            {
                sum[i]='0';
                carry='1';
 
            }
            else if(a[i]=='1' && b[i]=='1' && carry=='1')
            {
                sum[i]='1';
                carry='1';
 
            }
            else
                break;
        }
        
		printf("\nSum=%c%s",carry,sum);
		
		for(i=0;i<length;i++)
        {
            if(sum[i]=='0')
                complement[i]='1';
            else
                complement[i]='0';
        }
        
        if(carry=='1')
            carry='0';
        else
            carry='1';
        
		printf("\nChecksum=%c%s",carry,complement);
	}
	else {
		printf("\nWrong input strings");
	}
}

Checksum Program in C++

#include<iostream>
#include<string.h>
 
using namespace std;
 
int main()
{
    char a[20],b[20];
    char sum[20],complement[20];
    int i;
    
	cout<<"Enter first binary string\n";
    cin>>a;
    cout<<"Enter second binary string\n";
    cin>>b;
    
	if(strlen(a)==strlen(b))
    {
        char carry='0';
        int length=strlen(a);
        
		for(i=length-1;i>=0;i--)
        {
            if(a[i]=='0' && b[i]=='0' && carry=='0')
            {
                sum[i]='0';
                carry='0';
            }
            else if(a[i]=='0' && b[i]=='0' && carry=='1')
            {
                sum[i]='1';
                carry='0';
 
            }
            else if(a[i]=='0' && b[i]=='1' && carry=='0')
            {
                sum[i]='1';
                carry='0';
 
            }
            else if(a[i]=='0' && b[i]=='1' && carry=='1')
            {
                sum[i]='0';
                carry='1';
 
            }
            else if(a[i]=='1' && b[i]=='0' && carry=='0')
            {
                sum[i]='1';
                carry='0';
 
            }
            else if(a[i]=='1' && b[i]=='0' && carry=='1')
            {
                sum[i]='0';
                carry='1';
 
            }
            else if(a[i]=='1' && b[i]=='1' && carry=='0')
            {
                sum[i]='0';
                carry='1';
 
            }
            else if(a[i]=='1' && b[i]=='1' && carry=='1')
            {
                sum[i]='1';
                carry='1';
 
            }
            else
                break;
        }
        cout<<"\nSum="<<carry<<sum;
 
        for(i=0;i<length;i++)
        {
            if(sum[i]=='0')
                complement[i]='1';
            else
                complement[i]='0';
        }
        
		if(carry=='1')
            carry='0';
        else
            carry='1';
     
	    cout<<"\nChecksum="<<carry<<complement;
    }
    else
        cout<<"\nWrong input strings";
        
    return 0;
}

Remark underneath on the off chance that you have inquiries or discovered anything off base in above checksum program in C and C++.

Recent Posts

error: Alert: Content is protected!!