Thread: C program that prints the decimal equivalent of a binary integer.

  1. #1
    illmatic
    Guest

    C program that prints the decimal equivalent of a binary integer.

    Hi, I need help writing a C program that inputs an integer containing only 0s and 1s(binary integer) and prints its decimal equivalent. The hint is to use modulus and division operators to pick off the "binary" numbers digits one at a time from right to left. How would I do that? Thanks!

    Jay

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Assuming "n" contains only 1's and 0's mod 10 gives you the right most digit, then divide by ten "shifts" everything along 1 place, repeat as many times as you have digits.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    illmatic
    Guest
    Thanks vVv, but i haven't learned the functions of the<string.h) header file yet. I kinda have the concept of the program, but don't know where to loop and pick of each number from right to left.

    Jay

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    3
    Originally posted by adrianxw
    Assuming "n" contains only 1's and 0's mod 10 gives you the right most digit, then divide by ten "shifts" everything along 1 place, repeat as many times as you have digits.
    how exactly do I do that?

    Jay

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I'm not going to do your homework for you! Think about it, if "n" contains 101, then n%10 is 1, n/10 now is 10, so n%10 now ia 0, n/10 now is 1, n%10 is 1. You now have the 1's and 0's in right to left order, so multiply that up by the appropriate value, and sum them up.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Replies: 7
    Last Post: 08-19-2007, 08:10 AM
  4. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  5. Decimal to Binary Conversion program
    By acidbeat311 in forum C Programming
    Replies: 5
    Last Post: 01-12-2006, 10:26 PM