0

I think I've almost got it, but I feel like I'm go in circles trying to figure this out. The challenge to out cout without using strings or arrays. I took the number 56 as an example and 56 should equal 111000 this is not the case as it goes through fine till 7 then the number equals number*2 + number%2 makes it equal to 15 and outputs all 1's. Idk anymore, this is driving me to the moon and back.

#include <iostream>

using namespace std;

int main()
{
int number = 0;
int n = 1;
int x = n;
cin>>number;
cout<<n%2;
while(n <= number)
{
    if(n%2 == 0)
    {
        n = n*2;
        cout<<0;
    }
    else
    {
        n = n*2 + n%2;
        cout<<n%2;
    }
}
}
0

4 Answers 4

2

You can use the binary operator & to check if a single bit is 1 or 0.

for (int i=512; i>0; i/=2) {
    cout << ( ( number & i ) != 0 ) ;
}

Note that this WILL print leading 0's. Also, I'm assuming you only want to print positive integers.

Alternative:

for (int i=512; i>0; i/=2) {
    if (number >= i) {
        cout << 1;
        number -= i;
    } else {
        count << 0;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Good solution, but no need to call pow every time. You can start with i = 2^64 and divide by 2 (shift by one) every iteration.
I can't use math functions either, no pow :(
I've updated the solution with patros's suggestion and added an alternative method.
You can simply use cout << (bool)(number & i); aswell
ok I see what you did there, clever :D. Thanks a lot dxsmiley and patros I would have been stuck with this for quite some time.
0

You can use recursion

void decimal_to_binary(int decimal)
{
    int remainder = decimal % 2;
    if (decimal < 1)
        return;
    decimal_to_binary(decimal / 2);
    cout << remainder;
}

This function will take the decimal, get its remainder when divided to 2. Before it the function call itself again, it checks if the decimal is less than 1(probably 0) and return to execute the printing of 1's and 0's

1 Comment

OOOO this is cool and quite advanced for what where I'm at. I'll hold hold onto this for later, thank you.
0

I had this type of problem assigned to me recently. This code example work up to a maximum of 10 binary digits (per the problem guidelines) and keep prompting for input until 0 is entered (sentinel value). This can certainly be improved but the math is correct:

#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
 //Declare Variables
 int inputValue = 0;
 int workingValue = 0;
 int conversionSum = 0;

 //Begin Loop
 do{
     //Prompt for input
     cout << "Enter a binary integer (0 to quit): ";
     cin >> inputValue;

     //Reset Variables
     workingValue = inputValue;
     conversionSum = 0;

    //Begin processing input
    //10 digits max, so 10 iterations

    for (int i=0; i<10; i++) {
        //Check for non-binary entry
        if ((workingValue % 10) != 1 && (workingValue % 10 != 0)){
            cout << "Invalid!\n";
            workingValue = 0;
            conversionSum = 0;
            break;
           }

        //check to see if 2^i should be added to sum
        if (workingValue%2 == 1){
            conversionSum += pow(2,i);
            workingValue--;
           }
        //divide by 10 and continue loop
        workingValue= workingValue / 10;
    }

    //output results
    cout << "converted to decimal is: " << conversionSum << endl;

 }while (inputValue != 0);
}

Comments

0
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
    cout << "enter a number";
    int number, n, a=0;
    cin >> number;
    n = number;
    do 
    {
        n=n/2;
        a=a+1;  
    }
    while (n>=1);
    cout << "a is" << a;
    int c = a;
    int b = a;
    cout << "binary is";
    for(int i=0; i<=c; i++)
    {
        int k = number / pow(2,b);
        cout << k;
        number = number - k * pow(2,b);
        b = b-1;
    }
    return 0;
}

Although asked in C I have used C++. I have used the logic that if you have to convert decimal to binary we have to find the maximum power of 2 contained in the number which when added by 1 becomes the number of digit of required binary .. leftmost digit is the number of highest available power of 2 (ex in 8 highest power of 2 is 3 and 1 such is available)...then subtract this from the number and (ex 8-8=0)and search for number of next highest available power of 2 and so on.

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.