Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3.                               Online C++ Compiler.
  4.                Code, Compile, Run and Debug C++ program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <iostream>
  10.  
  11. using namespace std;
  12.  
  13. #define LEN 10
  14. void getBin(int value);
  15. int main()
  16. {
  17.     int n= 10;
  18.  
  19.     getBin(n);
  20.     return 0;
  21. }
  22.  
  23.  
  24. void getBin(int value) {
  25.     int temp = value;
  26.     int bin[LEN]= {0};
  27.     int r;
  28.     int index=0;
  29.     while(temp > 1) {
  30.         r = temp % 2;
  31.         temp /= 2;
  32.        
  33.         bin[index++] = r;
  34.     }
  35.     bin[index] = temp;
  36.    
  37.     cout << "origin: ";
  38.     for(int i=index;i>=0;i--) {
  39.         cout  << bin[i] << " ";
  40.     }
  41.     cout << endl;
  42.    
  43.     //reverse
  44.     cout << "reverse: ";
  45.     for(int i=0;i<=index;i++) {
  46.         cout << bin[i] << " ";
  47.     }
  48.     cout << endl;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement