0
# include <iostream>
# include <cmath>
using namespace std;
int main()
{

    int p;
    int n;
    int q;
    cin>>n;
    int r;
    r=0;
    for (int i=0,n; n>1; i=i+1,n=n/2)
    {

    p=n%2;
    q= p*(pow(10,i));
    r=r + q;
}

cout<<r;
system("pause");
return 0;
}

I am not supposed to use arrays. It compiles fine but when executed and a number is entered, it doesn't produce the desired results. For instance, when 22 is entered, it gives -2147483648 whereas the desired output would be 10110.

4
  • 1
    You are re-declaring variable n (uninitialized) inside the for loop!!! Commented Aug 30, 2015 at 4:54
  • @barakmanos yes, that worked. Thanks. Commented Aug 30, 2015 at 5:11
  • But, it only works upto 1023 which correctly gives 1111111111. when 1024 is entered it gives -2147483648. Why does that happen? Commented Aug 30, 2015 at 5:18
  • @UtkarshMittal Because of integer overflow. int variables can only hold numbers up to a certain value. 10000000000 is too big. As Dev says in his answer, your method of converting to binary is not very good. Commented Aug 30, 2015 at 5:28

2 Answers 2

1

your way is limited and not effient in converting to binary

you should use string it's more helpful and the range is big enough for any number

this is my code for decimal-to-binary

#include<iostream>
#include<string>
#include<stack>

using namespace std;

int main()
{
   long long n;
   string s,bin;
   stack<string> res;

   cin>>n;
   while(n>0)
   {
     if(n%2==0)
         s='0';
     else
         s='1';
     res.push(s);
     n=n/2;
   }
   while(!res.empty())
   {
     bin=bin+res.top();
     res.pop();
   }
   cout<<bin<<endl;

return 0;
}

I hope it will help you.

Sign up to request clarification or add additional context in comments.

Comments

0
int i=0,n;

should be

int i=0;

I don't know what you thought you were doing there, but what you are actually doing is declaring another variable n. Because the second n variable doesn't have a value the rest of the code doesn't work.

That's not the only problem with your code, but I'm sure you can figure out the rest.

3 Comments

Thanks, it worked. The other problem you are referring to-is it that the condition for loop to break should be n greater than 0 and not 1? Because after fixing that and what you stated, the code works perfectly! Moreover, is my algorithm optimized or there is a better way of doing this (without using arrays)?
But, it only works upto 1023 which correctly gives 1111111111. when 1024 is entered it gives -2147483648. Why does that happen?
@UtkarshMittal you can use long long to increase range of binary number that you can convert to but also it's limited string is the best way as my code in answer.

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.