Checksum Program in C and C++

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

A checksum is a error detection method in Data Communication. It is used for errors which may have been introduced during transmission or storage. It is usually applied to an installation file after it is received from the download server.

The actual procedure which yields the checksum, given a data input is called a checksum function or checksum algorithm.

Checksum method can only detect errors but is unable to correct the error.

In this method a checksum is calculated based on the given binary strings which is sent with the data as redundant bits. This data + checksum is received at receiver end and checksum is calculated again, if checksum is 0 it means no error in data received, else there exists some error in the received data.

For the purpose of this program we are finding checksum for 2 binary strings.

Checksum Algorithm

Take 2 binary input strings.

Do their binary sum to find out the checksum which will be sent to the destination or to the receiver.

In binary sum there are 6 cases:-

If both bits are 0 and carry is 0, sum=0 and carry=0

If both bits are 0 and carry is 1,sum=1 and carry=0

If both bits are 1 and carry is 0,sum=0 and carry=1

If both bits are 1 and carry is 1,sum=1 and carry=1

If either bit is 1 and carry is 0,sum=1 and carry=0

If either bit is 1 and carry is 1,sum=0 and carry=1

While doing the addition we have to add the binary strings from rightmost end i.e LSB to MSB.

When binary sum is done 1’s complement of it is taken by reversing 1’s to 0’s and vice versa.

The resulting 1’s complement is the Checksum.

Stop.

Checksum Program in C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

#include<stdio.h>

#include<string.h>

intmain()

{

chara[20],b[20];

charsum[20],complement[20];

inti,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);

charcarry='0';

for(i=length-1;i>=0;i--)

{

if(a[i]=='0'&&b[i]=='0'&&carry=='0')

{

sum[i]='0';

carry='0';

}

elseif(a[i]=='0'&&b[i]=='0'&&carry=='1')

{

sum[i]='1';

carry='0';

}

elseif(a[i]=='0'&&b[i]=='1'&&carry=='0')

{

sum[i]='1';

carry='0';

}

elseif(a[i]=='0'&&b[i]=='1'&&carry=='1')

{

sum[i]='0';

carry='1';

}

elseif(a[i]=='1'&&b[i]=='0'&&carry=='0')

{

sum[i]='1';

carry='0';

}

elseif(a[i]=='1'&&b[i]=='0'&&carry=='1')

{

sum[i]='0';

carry='1';

}

elseif(a[i]=='1'&&b[i]=='1'&&carry=='0')

{

sum[i]='0';

carry='1';

}

elseif(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++

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

#include<iostream>

#include<string.h>

usingnamespacestd;

intmain()

{

chara[20],b[20];

charsum[20],complement[20];

inti;

cout<<"Enter first binary string\n";

cin>>a;

cout<<"Enter second binary string\n";

cin>>b;

if(strlen(a)==strlen(b))

{

charcarry='0';

intlength=strlen(a);

for(i=length-1;i>=0;i--)

{

if(a[i]=='0'&&b[i]=='0'&&carry=='0')

{

sum[i]='0';

carry='0';

}

elseif(a[i]=='0'&&b[i]=='0'&&carry=='1')

{

sum[i]='1';

carry='0';

}

elseif(a[i]=='0'&&b[i]=='1'&&carry=='0')

{

sum[i]='1';

carry='0';

}

elseif(a[i]=='0'&&b[i]=='1'&&carry=='1')

{

sum[i]='0';

carry='1';

}

elseif(a[i]=='1'&&b[i]=='0'&&carry=='0')

{

sum[i]='1';

carry='0';

}

elseif(a[i]=='1'&&b[i]=='0'&&carry=='1')

{

sum[i]='0';

carry='1';

}

elseif(a[i]=='1'&&b[i]=='1'&&carry=='0')

{

sum[i]='0';

carry='1';

}

elseif(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";

return0;

}

Output

This article is submitted by Rahul Maheshwari. You can connect with him on facebook.

Comment below if you have queries or found anything incorrect in above checksum program in C and C++.