Thursday, 19 February 2015

Zeros in binary digits Solution.

/* Let N be an integer, you should calculate number of zeros in binary representation of N. N will begin with '1' in binary. e.g. zeros(5) = 1 because 5 = (101)b, thus it has 1 zero.*/

/*CodeFight*/

Solution:

#include<bits/stdc++.h>
using namespace std;
int z;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int zeros(int n) {
  if(n/2 != 0) {
      zeros(n/2);
  }
  if(n%2 == 0)z++;
  return z;
}
int main(){

    cout << zeros(5) << endl;;
}

No comments:

Post a Comment