UVA10931:

Parity

#include <iostream>
#include <string>
#include <bitset>


using namespace std;


string toBinary(int n){
	string s = "";


	while(n > 0){
		if(n % 2 != 0){
			s += '1';
		} else {
			s += '0';
		}	

		n /= 2; 
	}

	return string(s.rbegin(), s.rend());
}


int main(){
	int n; 

	while(cin >> n){
			
		if(n == 0){
			break;
		}


		 string binary = toBinary(n);

		 int count = 0;
		 for(int i = 0; i < binary.length(); i++){
		 	if(binary[i] == '1'){
		 		count++;
		 	}

		 }

		 cout << "The parity of " << binary << " is " << count << " (mod 2)." << endl;

	}

	return 0;
}
[Back to Listing Page]

Home

Posts

Projects

Github

Contact

janzzen

Developed by Janzzen Ang powered by Vercel