How do I determine the number of round numbers (to be explained below)?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Iruka07
    New Member
    • May 2010
    • 3

    #1

    How do I determine the number of round numbers (to be explained below)?

    NOTE: I am using Excel VBA.

    Please help me program the following problem:

    A positive integer n is said to be a ROUND NUMBER if the binary representation of n has as many or more zeros as ones. For example:

    The integer 9, written in binary form, is 1001. 1001 has two zeros and two ones; thus 9 is a ROUND NUMBER.

    The integer 26, written in binary form, is 11010. 11010 has two zeros and three ones; thus 26 is not a ROUND NUMBER.

    Write a program which will accept a positive integer k as input and print out the numbers of ROUND NUMBERS less than or equal to k in the following format:

    THERE ARE XXX ROUND NUMBERS LESS THAN OR EQUAL TO K

    Sample Run
    Input: K= 10
    Output: THERE ARE 5 ROUND NUMBERS LESS THAN OR EQUAL TO 10.

    Input: K= 100
    Output: THERE ARE 47 ROUND NUMBERS LESS THAN OR EQUAL TO 100.

    I have already managed to convert decimal to binary and to get the number of zeroes and number of ones. I just can't figure out how to do the following: 1) Do the entire thing for the whole range. Example, how do I get the binary equivalent of 5, 4, 3, 2, and 1 simultaneously? I can only get the binary equivalent of one decimal at a time. I've been doing this since last night... The deadline for this is later at 11:59 PM. Please do help me... Thank you very much!

    P.S. Please leave me a private message if you want to see the codes that I have managed to do so far. I read in your site that I should not post the codes that I have done so as to prevent plagiarism. Thank you!
  • Iruka07
    New Member
    • May 2010
    • 3

    #2
    Please guys I really really need your help... Whenever I use For & Next, things get messed up. So I don't know what else to use in order to loop the whole thing... Please help me! :(

    Comment

    • vb5prgrmr
      Recognized Expert Contributor
      • Oct 2009
      • 305

      #3
      Search for vb6 decimal binary with your friends (yahoo, google, ask, answers, bing) as I know there is code out there to do this conversion...



      Good Luck

      Comment

      • Iruka07
        New Member
        • May 2010
        • 3

        #4
        Originally posted by vb5prgrmr
        Search for vb6 decimal binary with your friends (yahoo, google, ask, answers, bing) as I know there is code out there to do this conversion...



        Good Luck
        Thank you for the reply! I have been doing so for like hours already and the codes are really complicated compared to what I have done... >_<

        Comment

        • Guido Geurs
          Recognized Expert Contributor
          • Oct 2009
          • 767

          #5
          dear,

          Just do a For...next from 1 to K , check If round number and enter in a counter with "counter=counte r+1" each time you have a round number.

          send a msgbox with ("THERE ARE " & counter & " ROUND NUMBERS LESS THAN OR EQUAL TO " & k)
          br,

          Comment

          Working...