[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

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

Posted on 2005-04-11
Medium Priority
176 Views
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.
0
Question by:cuong5985
• 2

LVL 1

Expert Comment

ID: 13760274
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

LVL 5

Expert Comment

ID: 13766003
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 Comment

ID: 13768761
#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

LVL 1

Accepted Solution

Mercurius0 earned 150 total points
ID: 13789320
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month19 days, 14 hours left to enroll