Thread: Simple binary digits separator + convert to decimal

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    3

    Simple binary digits separator + convert to decimal

    Hi, I'm very new to c programming. I was tasked to create a c program where the user enters the number of digits and the binary number, and the output will the binary number separated by 3 spaces and the decimal equivalent of the binary number.

    E.g
    Input:
    Enter the number of digits : 4
    Enter the number in binaries : 1001
    Output
    1 0 0 1
    The decimal equivalent is : 9

    I tried using pow function but I didn't go far..

  2. #2
    Registered User
    Join Date
    Feb 2021
    Posts
    3
    I also used an if loop so the number of digits is limited to a maximum of 9

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,469
    > Enter the number of digits : 4
    Presumably, you used scanf with %d to read this.

    > Enter the number in binaries : 1001
    What did you use to read this?

    One you have a sequence of 0 and 1, then you just need a loop over
    answer = answer * 2 + x, where x is your 0 or 1.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2021
    Posts
    3
    also using scanf with %d for the binaries
    Last edited by Bluerex99; 1 Day Ago at 08:02 PM. Reason: additional info

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,213
    That's pretty vague: a scanf call can have various format specifiers and various corresponding arguments. Show the code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    480
    Quote Originally Posted by Bluerex99 View Post
    also using scanf with %d for the binaries
    That is probably a mistake. You can make it work for small binary numbers, but it's not very logical and hard to error check.

    If you understand strings, you are best off entering the binary as a string (ascii 0 and ascii 1), then converting to machine representation (that's also a binary number, but the bits are the physical bits in memory), then convert to decimal.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-03-2012, 04:42 AM
  2. convert decimal to binary
    By tommytmh in forum C Programming
    Replies: 3
    Last Post: 11-24-2011, 06:38 AM
  3. Convert binary to decimal string
    By Devils Child in forum C# Programming
    Replies: 9
    Last Post: 01-26-2010, 07:37 AM
  4. Convert decimal to binary
    By planet_abhi in forum C Programming
    Replies: 1
    Last Post: 12-20-2002, 04:47 AM

Tags for this Thread