Rotated Number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • halo combat22
    New Member
    • Oct 2007
    • 24

    Rotated Number

    I need help on a project i was assigned.

    The digits 0, 1, and 8 look much the same if rotated 180 degrees on the page (turned upside down). Also, the digit 6 looks much like a 9, and vice versa, when rotated 180 degrees on the page. A multi-digit number may also look like itself when rotated on the page; for example 9966 and 10801 do, but 999 and 1234 do not.
    You are to write a program to count how many numbers from a given interval look like themselves when rotated 180 degrees on the page. For example, in the interval [1..100] there are six : 1, 8, 11, 69, 88, and 96.

    Your program should take as input two integers, m and n, which define the interval to be checked, 1 <= m <= n <= 32000. The output from your program is the number of rotatable numbers in the interval.

    You may assume that all input is valid.
    Sample Session User input is in italics.

    Enter the lower bound of the interval:

    1



    Enter the upper bound of the interval:

    100



    The number of rotatable numbers is:

    6
    I need a way to recognize these numbers.

    Any help is appreciated.
  • YarrOfDoom
    Recognized Expert Top Contributor
    • Aug 2007
    • 1243

    #2
    Use a for-loop to loop trough the numbers n to m.
    For checking a number, convert it to a string using CString(number), then use another for-loop to loop trough every character of your number.
    Say we call the position in the string of the current character a. Then first check if your character is one of the special cases (if not, you can abort the whole loop and check the next number). Second, check if the character at position StringMadeOutOf Number.length-(a-1) is the character you need (eg: 1 for 1, 6 for 9, 8 for 8, 9 for 6,...), if not, abort loop and you can check the next number. If the number gets trough the whole loop then it's one of the cases where it is the same if you turn it 180°.

    Yarr

    Comment

    • halo combat22
      New Member
      • Oct 2007
      • 24

      #3
      Code:
      Private Sub Command1_Click()
          Dim n, m, i As Integer
          Dim s As String
          m = txtm.Text
          n = txtn.Text
          For i = m To n
              For s = 1 To i - 1
         'where does cString go?
      End Sub
      Where does cString go?
      If it is a vb 2005 express thing i can't use it since i use vb6.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by halo combat22
        ... Where does cString go?
        If it is a vb 2005 express thing i can't use it since i use vb6.
        I believe that is VB.Net code (something later than VB6, anyway). However, I don't think we can go into too much detail, as we have a definite policy of not answering homework/classwork assignments for you.

        Suffice it to say that Cstring would (presumably) be converting a number into a string. You will need to make a real effort to solve the problem yourself. Show us what you write, and we'll see if we can help nudge you in the right direction.

        See the posting guidelines.

        Comment

        • YarrOfDoom
          Recognized Expert Top Contributor
          • Aug 2007
          • 1243

          #5
          Oww wait, it's not CString(), but just Str(), I mixed up a little with CInt()...

          Comment

          • halo combat22
            New Member
            • Oct 2007
            • 24

            #6
            Code:
            Private Sub Command1_Click()
            Dim s, sI, One, Two, Three, nCount As String
            Dim m, n, nLen As Integer
            Dim b As Boolean
            m = Text1.Text
            n = Text2.Text
            For i = m To n
                sI = Str(i)
                nLen = Len(sI)
                For j = 1 To nLen
                    Three = Mid(sI, nLen, 1)
                    If Three = 2 Or Three = 3 Or Three = 4 Or Three = 5 Or Three = 7 Then
                    b = False
                    Else
                    b = True
                    End If
                    One = Mid(sI, 1, 1)
                    Two = Mid(sI, nLen, 1)
                    If (b = True And One = Two) Then
                        nCount = nCount + 1
                    End If
                    
                  
                 
                    MsgBox (nCount)
            
                Next
            Next
            
            End Sub
            Well i got this far but it still hasn't worked? What is the problem?

            Comment

            • YarrOfDoom
              Recognized Expert Top Contributor
              • Aug 2007
              • 1243

              #7
              I should use "select case" instead of "if", and you shouldn't place the msgbox inside a loop.

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by halo combat22
                ...Well i got this far but it still hasn't worked? What is the problem?
                Can you be more specific than "hasn't worked"?

                Also, I think you will find it much easier if you first work out in detail the exact process you are going to use, before you try to write the code. That's where "pseudo-code" comes in very handy. For example...

                Code:
                For each number [I]N[/I] in the range 1 to 100
                  For Each digit of [I]N[/I]
                    If digit is 1 or 8 or 0 Then
                      Blah...
                    Else If digit is 6 or 9 Then
                      Blah...
                    Else
                      Blah...
                    End If
                  Loop
                Loop
                Last edited by Killer42; Nov 12 '07, 03:19 AM.

                Comment

                Working...