4 Replies - 924 Views - Last Post: 09 February 2011 - 02:49 PM Rate Topic: -----

#1 benji247  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-February 11

Help with creating program to display all factors of a given number

Posted 09 February 2011 - 02:11 PM

Hi Guys

I am working on a simple program to return all factors of a given input integer. Unfortunately I am stuck. THe code is

Dim x As Integer
        x = txtInput.Text
        Dim factor As Integer
        factor = CInt(txtInput.Text) - 1
        Dim i As Integer
        i = 1


        While factor > 0
            Do Until i = x
                If factor * i = x Then
                    ListBox1.Items.Add(factor)
                    i = i + 1
                Else
                    i = i + 1
                End If
            Loop
            factor = factor - 1
        End While



x is the input number and i is the increment to see whether it is a factor. factor is a factor.

Can anybody shed some light on where i am going wrong? When it runs it does not appear to do anything. I have stepped into the code and it appears to be running correctly but there is no output.


Thanks for your help!

P.S. I know I have not yet handled outputs which have already been returned.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with creating program to display all factors of a given number

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,199
  • Joined: 25-September 09

Re: Help with creating program to display all factors of a given number

Posted 09 February 2011 - 02:26 PM

You need to reset i when you reduce factor

Lets do this on paper:
x = 5
factor = 4
i = 1
4*1 <> 5
4*2 <> 5
4*3 <> 5
4*4 <> 5
3*1 <> 5
...
3.4 <> 5
...
1*1<> 5
...
1*4 <> 5

It can never equal the value of x...

Why not?
Was This Post Helpful? 1
  • +
  • -

#3 benji247  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-February 11

Re: Help with creating program to display all factors of a given number

Posted 09 February 2011 - 02:30 PM

Hi

Thanks for your reply. It certainly has given me something to ponder.

With x=5 the only factors are 5 and 1. is that correct?

Therefore it can only return an output if we set the factor variable to 5 at some point.

Am I on the right track?
Was This Post Helpful? 0
  • +
  • -

#4 benji247  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-February 11

Re: Help with creating program to display all factors of a given number

Posted 09 February 2011 - 02:42 PM

View PostCharlieMay, on 09 February 2011 - 02:26 PM, said:

You need to reset i when you reduce factor

Lets do this on paper:
x = 5
factor = 4
i = 1
4*1 <> 5
4*2 <> 5
4*3 <> 5
4*4 <> 5
3*1 <> 5
...
3.4 <> 5
...
1*1<> 5
...
1*4 <> 5

It can never equal the value of x...

Why not?



Thank you so much CharlieMay. I just read your message again and tried it, presto...it worked! Cant believe I missed that.

TYSM

Ben
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,199
  • Joined: 25-September 09

Re: Help with creating program to display all factors of a given number

Posted 09 February 2011 - 02:49 PM

Well, the i not being reset is your main problem. By never comparing against your number as the factor, the only thing that wouldn't work is when you enter a prime number.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1