Read a number and convert it to binary string???

Can someone help me what should i do with this program, just give me an idea??

Read a positive integer  and convert it to a binary string:

Enter a positive number:  20

20 in decimal is 1 0100 in binary

user will enter 0 to exit the program.

Handle negative numbers and numbers that are too large. Make sure the output is properly spaced.
Who is Participating?

Commented:
If you want to use this bitset, try

#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <bitset>

main()
{
int integer, binary;
using namespace std ;

cout<<"Enter a positive integer: ";
cin >>integer;

if(integer==0)
{
cout << "Exiting";
// Handle errors how you see fit...
exit(0);   // Will exit your program
}
else
{
bitset<16> binary=integer;
cout<<integer<<" in decimal is "<<binary<<" in binary.";
}
return 0;
}

One disadvantage of bitset is that you can't use a variable to set the number of bits
However, 16 will cover any integer you will be getting, providing you use a console-application
0

Commented:
devide the input-number with the powers of 2 and then do it modulo 2 (see example below)

(20/1)%2=0
(20/2)%2=0
(20/4)%2=1
(20/8)%2=0
(20/16)%2=1

Use a for-loop for this

0

Commented:
in C++ StandartTemplateLibrary, there are special container (bitset) which, i think, help you. Here the ex.:

#include <iostream>
#include <bitset> // bitset header file

main() {
using namespace std ;
bitset<10> b = 20 ;
cout << b << "\n" ; // will print 0000010100
}
0

Author Commented:
#include <iostream.h>
#include <stdlib.h>

int main()
{
int interger;
int size;

cout<<"Enter a positive integer: ";
cin >>interger, binary;
cout<<interger<<" in decimal is "<<binary<<" in binary."<<endl;

if (0 == size)
{
cout << "Exiting" << endl;
// Handle errors how you see fit...
exit(0);   // Will exit your program
}

for (

cout<< "\n";
}

system("PAUSE");
return 0;
}

This is what i did, but still finish yet, can someone help me to finish it
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.