C++ - Decimal to binary converting

后端 未结 29 3645
一向
一向 2020-11-28 18:53

I wrote a \'simple\' (it took me 30 minutes) program that converts decimal number to binary. I am SURE that there\'s a lot simpler way so can you show me? Here\'s the code:<

29条回答
  •  囚心锁ツ
    2020-11-28 19:54

    HOPE YOU LIKE THIS SIMPLE CODE OF CONVERSION FROM DECIMAL TO BINARY
    
    
      #include
        using namespace std;
        int main()
        {
            int input,rem,res,count=0,i=0;
            cout<<"Input number: ";
            cin>>input;`enter code here`
            int num=input;
            while(input > 0)
            {
                input=input/2;  
                count++;
            }
    
            int arr[count];
    
            while(num > 0)
            {
                arr[i]=num%2;
                num=num/2;  
                i++;
            }
            for(int i=count-1 ; i>=0 ; i--)
            {
                cout<<" " << arr[i]<<" ";
            }
    
    
    
            return 0;
        }
    

提交回复
热议问题