Bostonpublic
asked on
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?
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?
ASKER
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
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
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.
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.
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
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
ASKER
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
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
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
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
ASKER
thank you so much:)
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.
ASKER
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?
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?
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
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
ASKER
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
this is what I have...
Option Explicit
Dim inI As Integer
Dim inCount As Integer
Dim inCount As Integer
Move this line in to the procedure
Move this line in to the procedure
Sorry about that.....I guess I forgot to define that one in the procedure (I had no Option explicit, so it worked anyways).
ASKER
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?
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?
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
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
ASKER
thank you...one sec ill show you
http://pic7.picturetrail.com/VOL193/982072/1824368/21927058.jpg <-before
http://pic7.picturetrail.com/VOL193/982072/1824368/21927047.jpg <-after
http://pic7.picturetrail.com/VOL193/982072/1824368/21927058.jpg <-before
http://pic7.picturetrail.com/VOL193/982072/1824368/21927047.jpg <-after
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!
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!
ASKER
thank you
ASKER
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.
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
if inCount < 3 then
Label1.Caption = Label1.Caption & vblf & "This number is prime"
end if
ASKER
oh ok thanks
ASKER
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?
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?
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
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
ASKER
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.
but other than that it worked, thank you so much.
ASKER
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
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
What is your email.....i'll send you what I have here that i've been tinkering with.
ASKER
it's elemenopee3@hotmail.com :)
ASKER
it is still saying every number is prime even if they aren't.
ASKER
it is still saying every number is prime even if they aren't.
ok, check your email......subject is "Factors"
ASKER
i am downloading it now
ASKER
it's taking forever do you use msn? maybe it'll send faster?
ASKER
actually nm let me look at it =)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
it worked thank you so so much.
ASKER
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