rorschack

Decimal to binary in C++

Mar 25th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     unsigned int n;
  8.     char num[200]="";
  9.     cout << "Enter a positive decimal number: ";
  10.     cin >> n;
  11.     while(n>0) {
  12.         if(n%2==0)
  13.             strcpy(num,strcat(num,"0"));
  14.         else
  15.             strcpy(num,strcat(num,"1"));
  16.         n=n/2;
  17.     }
  18.     int len=strlen(num);
  19.     while(len>0) {
  20.         cout << num[len-1];
  21.         len--;
  22.     }
  23.     cout << endl;
  24.     return 0;
  25. }
Add Comment
Please, Sign In to add comment