ahmed0saber

Convert to binary in C++

Nov 12th, 2020
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.25 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int n,i=0,num,t;
  6.     cout<<"Enter a number : ";
  7.     cin>>n;
  8.     while (n!=0)
  9.     {
  10.         if (n%2==1)
  11.         {
  12.             t=1;
  13.             for(int j=0;j<i;j++)
  14.             {t=t*10;}
  15.             num=num+t;
  16.         }
  17.         n=n/2;
  18.         i++;
  19.     }
  20.     cout<<num;
  21. }
Advertisement
Add Comment
Please, Sign In to add comment