C++ Program to Find XOR of All Digits in Binary Sequence

This is a C++ Program to Find the XOR of all Digits in a Given Binary Sequence.

Problem Description

The program takes a binary sequence and finds the XOR of all the digits. XOR gives a high value when there are odd number of 1s in the input.

Problem Solution

1. The program takes a binary number.
2. Using a while loop, XOR of all digits in the number is calculated by counting the number of 1s.
3. If number of 1s is odd, then result is 1.
4. Else, it is 0.
5. The result is printed.
6. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Find the XOR of all Digits in a Given Binary Sequence. The program output is shown below.

  1. #include <iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int num, temp, res, x, i = 0;
  6.     cout << "Enter a binary number : ";
  7.     cin >> num;
  8. 	temp = num;
  9.     while (temp != 0)
  10.     {
  11.         x = temp % 10;
  12.         if (x == 1)
  13.             i++;
  14.         temp = temp / 10;
  15.     }
  16.     if (i == 0)
  17.         res = 0;
  18.     if (i % 2 == 0)
  19.         res = 0;
  20.     else
  21.         res = 1;
  22.     cout << "\nExclusive OR of " << num << " is : " << res;
  23.     return 0;	
  24. }
Program Explanation

1. The user is asked to enter a binary number and it is stored in the variable num.
2. num is copied to a temporary variable temp. The variable ‘i’ is initialized as 0.
3. Using a while loop, temp is divided by 10 and the remainder is stored in the variable ‘x’.
4. If x is 1, the value of ‘i’ is incremented.
5. The loop terminates when temp is equal to 0.
6. If the value of i is 0 or even, then XOR of all the digits is 0 since there are 0 or even number of 1s.
7. Else, the result is 1.
8. The result is then printed.

advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter a binary number : 1110000011
Exclusive OR of 1110000011 is : 1
 
Case 2 :
Enter a binary number : 11
Exclusive OR of 11 is : 0
 
Case 3 :
Enter a binary number : 10101
Exclusive OR of 10101 is : 1

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.