//
// CS 202 demonstration program 05
// Written by Dave Williams for 9/23/03 class 
//
// Program to convert a decimal value to binary (max 16 bit)
//

void Dec2bin(long int, long int);

#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

int main()   
{   

   long int a;
   clock_t start, stop;
     
   cout << "\n\nProgram to convert a decimal value to 16 bit binary.";
   
   do
   {
      cout << "\n\n\n\nInput value (-1 to quit) : ";
      cin >> a;

      if (a == -1)                  
         exit(0);

      if (a > 65535)
         cout << "\nCan't do that - more than 16 bits required!";         
         
      else
      {   
         start = clock();
              
         cout << "\n\n" << a << " decimal = "; 

         Dec2bin(a, 32768);
         
         cout << " binary.";
              
         stop = clock();

         cout << endl << endl << "Total runtime: "
              << (stop - start) / (CLOCKS_PER_SEC / double (1000.)) << " ms.";
      }
      
   } while(1);

   return 0;       
}


void Dec2bin(long int n, long int factor) 
{    
       
   if (factor < 1)
      return;
      
   if ((n - factor) < 0)  
      cout << "0";                     
                      
   else
   {
      cout << "1";       
      n = n-factor;
   } 
                       
   Dec2bin(n, factor/2);   // call recursively  
                
}

// !
// !
// !