Check Even parity or Odd parity from a integer Number or Binary number in c++


At first we need to know what is Even parity and what is Odd parity ? 
Even parity represent that how many 1 have in a binary code. if the total number of 1 is even so we called it Even parity.
other hand Odd parity represent that how many 1 have in a binary code . if the total number of 1 is odd so we called it Odd parity.

Example :

10111011 = Total number of 1 is 6 so it is Even parity .
10011101= Total number of 1 is 5 so it is Odd parity .

Implementation :

#include<bits/stdc++.h>

using namespace std;
int main(){
  int n,j,i=0,counter=0;
  cout<<"Enter a integer Number: ";
  cin>>n;
  int reminder[100];
  while(n>0)
  {

      reminder[i]=n%2;

      n=n/2;
      i++;

  }
  cout<<"Binary number is : ";
  for(j=i-1;j>=0;j--)
  {
      cout<<reminder[j];
      if(reminder[j]==1)
      {
          counter++;
      }
  }
  cout<<endl;
  if(counter%2==0)
  {
      cout<<"Even parity"<<endl;
  }
  else
  {
      cout<<"Odd Parity"<<endl;
  }

return 0;

}

Output:



Check Even parity or Odd parity from a integer Number or Binary number in c++ Check Even parity or Odd parity from a integer Number or Binary number in c++ Reviewed by Saroar Zahan Sojib on February 23, 2019 Rating: 5

No comments:

ads
Powered by Blogger.