binary

This is a discussion on binary within the C++ Programming forums, part of the General Programming Boards category; hi! I need to write a progam that will convert a number to binary. Example user enters number between 0 ...

  1. #1
    Registered User mike's Avatar
    Join Date
    Aug 2001
    Posts
    12

    Question binary

    hi!
    I need to write a progam that will convert a number to binary.
    Example
    user enters number between 0 and 15
    output is the value of the number returned to screen
    and then the output in binary returned to the screen

    thank you!

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Here's one solution -

    #include <iostream>
    #include <bitset>

    using namespace std;

    Code:
    #include <iostream> 
    #include <bitset>
    
    using namespace std;
    
    int main() 
    { 
    
        int i =0;
    
        do
        {
            cin >>i;
        }while (i<0 || i>15);
    
        //need four bits to represent 0-15
    
        bitset <4> j = i;
        cout << i << " = " << j << endl;
    
    
        return 0;
    } 
    However, if this is a homework assignment then I doubt that this will be the answer you're looking for. Let's see what you've done so far.

  3. #3
    zoo
    Guest
    Are you allowed to use itoa()?

  4. #4
    Registered User mike's Avatar
    Join Date
    Aug 2001
    Posts
    12
    Originally posted by zoo
    Are you allowed to use itoa()?

    Thanks for your reply. some has already helped me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 01:02 PM
  2. Replies: 0
    Last Post: 11-04-2006, 10:07 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21