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

Page 1 of 1

## 4 Replies - 1113 Views - Last Post: 09 February 2011 - 02:49 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=215075&amp;s=450d35a1d79c00b4916c5388f8139093&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 benji247

Reputation: 0
• 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
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.

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

• This space intentionally left blank

Reputation: 1720
• Posts: 5,636
• 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?

### #3 benji247

Reputation: 0
• 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?

### #4 benji247

Reputation: 0
• 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

CharlieMay, 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

### #5 CharlieMay

• This space intentionally left blank

Reputation: 1720
• Posts: 5,636
• 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.