# I can't figure out what is going wrong?

I am currently trying to write a program that will accept a positive integer between 50 and 300 and count the number of factors the number has.  Output the numbers and the number of factors in a sentence.  Have the program decide whether the number is prime?

This is what I have so far, but it keep saying every number isn't prime, even if they are.
Also when I run the program if the number is under 50 or 300 it still shows the factors in the picture box. How can I fix this?
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Author Commented:
oops forgot the post the code lol

Option Explicit
Dim Num As Integer, C As Integer, Counter As Integer, Response As Integer, itself As Integer

Private Sub cmdEnd_Click()
End
End Sub

Private Sub Text1_Change()

End Sub

Private Sub cmdGo_Click()
For C = 50 To 300
Next C
Num = Val(txtFactors.Text)
Counter = 0
For C = 1 To Num
If Num Mod C = 0 Then
Counter = Counter + 1
PicAns.Print C
End If
Next C
PicAns.Print "There were"; Counter; "Factors"
If txtFactors.Text > 300 Then
Response = MsgBox("You must enter a number smaller than 300", vbOKOnly + vbInformation, " Error")
ElseIf txtFactors.Text < 50 Then
Response = MsgBox("You must enter a number larger than 50", vbOKOnly + vbInformation, " Error")
End If
If Val(txtFactors.Text * 1 And itself) Then
PicAns.Print "This is a prime number"
Else: PicAns.Print "This isn't a prime number"
End If

End Sub

Private Sub cmdReset_Click()
txtFactors.Text = ""
frmFactors.Cls
PicAns.Cls
End Sub

Private Sub Picture1_Click()

End Sub

Private Sub Pic_Click()

End Sub

0
Author Commented:
oops forgot the post the code lol

Option Explicit
Dim Num As Integer, C As Integer, Counter As Integer, Response As Integer, itself As Integer

Private Sub cmdEnd_Click()
End
End Sub

Private Sub Text1_Change()

End Sub

Private Sub cmdGo_Click()
For C = 50 To 300
Next C
Num = Val(txtFactors.Text)
Counter = 0
For C = 1 To Num
If Num Mod C = 0 Then
Counter = Counter + 1
PicAns.Print C
End If
Next C
PicAns.Print "There were"; Counter; "Factors"
If txtFactors.Text > 300 Then
Response = MsgBox("You must enter a number smaller than 300", vbOKOnly + vbInformation, " Error")
ElseIf txtFactors.Text < 50 Then
Response = MsgBox("You must enter a number larger than 50", vbOKOnly + vbInformation, " Error")
End If
If Val(txtFactors.Text * 1 And itself) Then
PicAns.Print "This is a prime number"
Else: PicAns.Print "This isn't a prime number"
End If

End Sub

Private Sub cmdReset_Click()
txtFactors.Text = ""
frmFactors.Cls
PicAns.Cls
End Sub

Private Sub Picture1_Click()

End Sub

Private Sub Pic_Click()

End Sub

0
Commented:
This guy has a prime number function:
http://www.geocities.com/SiliconValley/Network/4328/visual.htm

Another guy did a VB prog that will print all the prime numbers up to a number the user specifies, you may be able to use this:
http://www.codeguru.com/columns/VB/PK061802.html

Hope one of these can help ya.
0
Commented:
Here's my code that will print out all the prime numbers between 50 and 300.  I hope this is of some use.  Let me know if you have any more problems with it.

Private Sub Command1_Click()
Dim inCounter(50 To 300) As Integer
For inI = 50 To 300
For inJ = 1 To inI
If inI Mod inJ = 0 Then inCounter(inI) = inCounter(inI) + 1
Next inJ
If inCounter(inI) < 3 Then Picture1.Print inI
Next inI
End Sub
0
Author Commented:
it is only showing the fractions from 50 to whatever number I chose.
Dim inCounter(50 To 300) As Integer
For inI = 50 To 300
For inJ = 1 To inI
If inI Mod inJ = 0 Then inCounter(inI) = inCounter(inI) + 1
Next inJ
If inCounter(inI) < 50 Then PicAns.Print inI
Next inI
0
Commented:
Here's a fixed version that will do what you want.

Private Sub Command1_Click()
Dim inCounter(50 To 300) As Integer
Dim inNum As Integer
Dim inFactor() As Integer
inNum = CInt(Text1.Text)

For inI = 1 To inNum
If inNum Mod inI = 0 Then
inCount = inCount + 1
ReDim Preserve inFactor(1 To inCount)
inFactor(inCount) = inI
End If
Next inI

Dim stSentence As String
stSentence = "Factors are: "

For inI = 1 To inCount
stSentence = stSentence & " " & inFactor(inI)
stSentence = stSentence & ","
Next inI

stSentence = stSentence & " which is a total of " & inCount & " Factors."

Label1.Caption = stSentence
End Sub
0
Author Commented:
thank you so much:)
0
Commented:
Then, as you can see, if the inCount value is equal to 2, then the number is prime.  You can add statements as you wish to do whatever you want from there.
0
Author Commented:
Oh quick question,
Why for when I do the factors of 71 it works but if i try it again it adds zeros to the forum.?
And how can I get the numbers to go vertically instead of horizontally?
0
Commented:
It doesn't do that on mine (the zeros that is).  Here is code for the vertical thing.

Private Sub Command1_Click()
Label1.Alignment = 2
Dim inCounter(50 To 300) As Integer
Dim inNum As Integer
Dim inFactor() As Integer
inNum = CInt(Text1.Text)

For inI = 1 To inNum
If inNum Mod inI = 0 Then
inCount = inCount + 1
ReDim Preserve inFactor(1 To inCount)
inFactor(inCount) = inI
End If
Next inI

Dim stSentence As String
stSentence = "Factors are: "

For inI = 1 To inCount
stSentence = stSentence & vbLf & inFactor(inI)
Next inI

stSentence = stSentence & vbLf & " which is " & inCount & " Factors."

Label1.Caption = stSentence
End Sub
0
Author Commented:
hmm maybe I have something wrong in my general statement?

this is what I have...
Option Explicit
Dim inI As Integer
Dim inCount As Integer
0
Commented:
Dim inCount As Integer

Move this line in to the procedure
0
Commented:
Sorry about that.....I guess I forgot to define that one in the procedure (I had no Option explicit, so it worked anyways).
0
Author Commented:
thank you so much.

it works now.
when i error proof it like when they put a number greater than 300 or less than 50 i would just continue at the end of my code right?

Also how come my box where the factors are going jumped down?
0
Commented:
Try this code.  It may be very suitable for you.  About the box jumping down, I don't know what that is.  Can you describe it a bit better?

Option Explicit
Dim inI As Integer
Private Sub Text1_Change()
Dim inCount As Integer
If IsNumeric(Text1.Text) Then
If Text1.Text > 49 And Text1.Text < 301 Then
Label1.Alignment = 2
Dim inCounter(50 To 300) As Integer
Dim inNum As Integer
Dim inFactor() As Integer
inNum = CInt(Text1.Text)

For inI = 1 To inNum
If inNum Mod inI = 0 Then
inCount = inCount + 1
ReDim Preserve inFactor(1 To inCount)
inFactor(inCount) = inI
End If
Next inI

Dim stSentence As String
stSentence = "Factors are: "

For inI = 1 To inCount
stSentence = stSentence & vbLf & inFactor(inI)
Next inI

stSentence = stSentence & vbLf & " which is " & inCount & " Factors."

Label1.Caption = stSentence
End If
End If
End Sub
0
Author Commented:
0
Commented:
Private Sub FindPrimes()
Dim i as Long
Dim j as Long
Dim iRoot as Long
Dim NoFactors as Long

For i = 50 to 500
iRoot = CLng(sqr(i)) ' SqureRoof of i
NoFactors = 0
For j = 2 to iRoot
if (i MOD j = 0) then
NoFactors = NoFactors + 1
Exit For
End if
Next j
if (NoFactors = 0) then
Print i; " is a prime number."
End if
Next i

End Sub

Good Luck!
0
Author Commented:
thank you
0
Author Commented:
oh darnit i dont think that is what the teacher wanted. I think she wanted the user to be able to enter a number between 50 and 300 and the program is to find the factors of it and say whether or not the number is prime. And somehow error proof it in case the user enters a number less than 50 or greater than 300.
0
Commented:
Well then add a statement such as the following at the end of the code.

if inCount < 3 then
Label1.Caption = Label1.Caption & vblf & "This number is prime"
end if
0
Author Commented:
oh ok thanks
0
Author Commented:
Dim i As Long
Dim j As Long
Dim iRoot As Long
Dim NoFactors As Long
For i = 50 To 300
iRoot = CLng(Sqr(i)) ' SqureRoof of i
NoFactors = 0
For j = 2 To iRoot
If (i Mod j = 0) Then
NoFactors = NoFactors + 1
Exit For
End If
Next j
If (NoFactors = 0) Then
Print i; " is a prime number."
End If
Next i
If inCount < 3 Then
PicAns.Print PicAns & vbLf & "This number is prime"
End If

does that look right?
0
Commented:
No, cause that does what I did for you in the first place.  It goes through and finds all the prime numbers.  Use this.

Option Explicit
Dim inI As Integer
Private Sub Text1_Change()
Dim inCount As Integer
If IsNumeric(Text1.Text) Then
If Text1.Text > 49 And Text1.Text < 301 Then
Label1.Alignment = 2
Dim inCounter(50 To 300) As Integer
Dim inNum As Integer
Dim inFactor() As Integer
inNum = CInt(Text1.Text)

For inI = 1 To inNum
If inNum Mod inI = 0 Then
inCount = inCount + 1
ReDim Preserve inFactor(1 To inCount)
inFactor(inCount) = inI
End If
Next inI

Dim stSentence As String
stSentence = "Factors are: "

For inI = 1 To inCount
stSentence = stSentence & vbLf & inFactor(inI)
Next inI

stSentence = stSentence & vbLf & " which is " & inCount & " Factors."

Label1.Caption = stSentence
Label1.caption = label1.caption & vblf & "This Number is Prime"
End If
End If
End Sub
0
Author Commented:
do you have any idea why the box is still moving down on me though?

but other than that it worked, thank you so much.

0
Author Commented:
0
Commented:
Sure, see if this works.  I think i know what was wrong

Option Explicit
Dim inI As Integer
Private Sub Text1_Change()
Dim inCount As Integer
If IsNumeric(Text1.Text) Then
If Text1.Text > 49 And Text1.Text < 301 Then
Label1.Alignment = 2
Dim inCounter(50 To 300) As Integer
Dim inNum As Integer
Dim inFactor() As Integer
inNum = CInt(Text1.Text)
inCount = 0
For inI = 1 To inNum
If inNum Mod inI = 0 Then
inCount = inCount + 1
ReDim Preserve inFactor(1 To inCount)
inFactor(inCount) = inI
End If
Next inI

Dim stSentence As String
stSentence = "Factors are: "

For inI = 1 To inCount
stSentence = stSentence & vbLf & inFactor(inI)
Next inI

stSentence = stSentence & vbLf & " which is " & inCount & " Factors."

Label1.Caption = stSentence
Label1.Caption = Label1.Caption & vbLf & "This Number is Prime"
End If
End If
End Sub
0
Commented:
What is your email.....i'll send you what I have here that i've been tinkering with.
0
Author Commented:
it's elemenopee3@hotmail.com :)
0
Author Commented:
it is still saying every number is prime even if they aren't.
0
Author Commented:
it is still saying every number is prime even if they aren't.
0
Commented:
ok, check your email......subject is "Factors"
0
Author Commented:
0
Author Commented:
it's taking forever do you use msn? maybe it'll send faster?
0
Author Commented:
actually nm let me look at it =)
0
Commented:
yeah I have msn
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
it worked thank you so so much.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.