/* 
 * Michael Gousie
 * COMP 318  Spring 2026
 * bin.cpp
 *
 * Convert given decimal integer to binary.
 * Shows recursion -> lg n
 */

#include<iostream>

using namespace std;

void bin (int);

int sum = 0;   // total number of operations (look! a global!)

int main () {
   int x;      // integer to convert to binary

   cout << "Enter decimal value : ";
   cin  >> x;

   cout << "Binary equivalent is: ";
   bin (x);
   cout << endl;

   cout << "Number of operations: " << sum << endl;
   return 0;
}

// Recursive function to convert and display the binary value
void bin (int n) {
   if (n < 2) {
      cout << n;
      sum++;				   
   }
   else {
      bin (n / 2);	
      cout << n % 2; 
      sum++;
   }
}
