C++ - Decimal to binary converting

后端 未结 29 3652
一向
一向 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条回答
  •  旧时难觅i
    2020-11-28 19:41

    This is a more simple program than ever

    //Program to convert Decimal into Binary
    #include
    using namespace std;
    int main()
    {
        long int dec;
        int rem,i,j,bin[100],count=-1;
        again:
        cout<<"ENTER THE DECIMAL NUMBER:- ";
        cin>>dec;//input of Decimal
        if(dec<0)
        {
            cout<<"PLEASE ENTER A POSITIVE DECIMAL";
            goto again;
        }
        else
            {
            cout<<"\nIT's BINARY FORM IS:- ";
            for(i=0;dec!=0;i++)//making array of binary, but reversed
            {
                rem=dec%2;
                bin[i]=rem;
                dec=dec/2;
                count++;
            }
            for(j=count;j>=0;j--)//reversed binary is printed in correct order
            {
                cout<

提交回复
热议问题