-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParity_Checker.cpp
More file actions
73 lines (61 loc) · 1.02 KB
/
Parity_Checker.cpp
File metadata and controls
73 lines (61 loc) · 1.02 KB
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
//Name - Mohit Jaiswal
//Parity Checker
#include <iostream>
#include <string.h>
using namespace std;
class parity_checker
{
public:
int j=0, count=0;
string s;
int b[20];
void input();
void parity_check();
};
void parity_checker::input()
{
cout<<"Enter the String - "<<endl;
cin>>s;
}
void parity_checker::parity_check()
{
int n=s.length();
cout<<"\nString length is = "<<n<<endl;
for(int i=0;i<n;i++)
{
int val=int(s[i]);
cout<<s[i]<<" = "<<val<<endl; //Takes the ASCII value
j=0;
while(val>0)
{
int p=val%2; //Binary conversion
b[j]=p;
val=val/2;
j++;
}
}
cout<<"\n";
for(int i=j-1; i>=0; i--)
{
cout<<b[i];
if(b[i]==1)
{
count++; //Calculate no. of 1
}
}
if(count%2==0) //Check parity
{
cout<<"\nEven Parity"<<endl;
}
else
{
cout<<"\nOdd parity"<<endl;
}
}
int main()
{
parity_checker p;
p.input();
p.parity_check();
return 0;
}