Advertisement
VouivreCritique

C++ Binary Algorithm

Nov 1st, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     ofstream myFile;
  9.     myFile.open("binary.doc");
  10.  
  11.     int bits = 1;
  12.     cout << "Enter number of bits as an integer greater than 0: ";
  13.     cin >> bits;
  14.  
  15.     int maxPow = pow(2,bits);
  16.  
  17.     for (int i = 0; i < maxPow; i++) {
  18.             int currPow = maxPow;
  19.             while (currPow != 1) {
  20.                 (i % currPow < currPow/2) ? myFile << "0" : myFile << "1";
  21.                 currPow /= 2;
  22.             }
  23.         myFile << endl;
  24.     }
  25.  
  26.     cout << "Printed binary numbers 0-" << maxPow-1 << " to binary.doc\n\n";
  27.  
  28.     myFile.close();
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement