Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

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

Posted on 2003-03-19
Medium Priority
187 Views
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?
0
Question by:Bostonpublic
• 21
• 13
• +1

Author Comment

ID: 8169800
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 Comment

ID: 8169812
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

Expert Comment

ID: 8169867
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

LVL 2

Expert Comment

ID: 8169897
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 Comment

ID: 8169919
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

LVL 2

Expert Comment

ID: 8169966
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 Comment

ID: 8169985
thank you so much:)
0

LVL 2

Expert Comment

ID: 8169997
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 Comment

ID: 8170002
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

LVL 2

Expert Comment

ID: 8170063
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 Comment

ID: 8170093
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

LVL 2

Expert Comment

ID: 8170151
Dim inCount As Integer

Move this line in to the procedure
0

LVL 2

Expert Comment

ID: 8170158
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 Comment

ID: 8170172
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

LVL 2

Expert Comment

ID: 8170223
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

LVL 11

Expert Comment

ID: 8170583
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 Comment

ID: 8170597
thank you
0

Author Comment

ID: 8170621
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

LVL 2

Expert Comment

ID: 8170685
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 Comment

ID: 8170687
oh ok thanks
0

Author Comment

ID: 8170707
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

LVL 2

Expert Comment

ID: 8170729
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 Comment

ID: 8170759
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 Comment

ID: 8170783
0

LVL 2

Expert Comment

ID: 8170792
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

LVL 2

Expert Comment

ID: 8170797
What is your email.....i'll send you what I have here that i've been tinkering with.
0

Author Comment

ID: 8170817
it's elemenopee3@hotmail.com :)
0

Author Comment

ID: 8170822
it is still saying every number is prime even if they aren't.
0

Author Comment

ID: 8170827
it is still saying every number is prime even if they aren't.
0

LVL 2

Expert Comment

ID: 8170850
ok, check your email......subject is "Factors"
0

Author Comment

ID: 8170865
0

Author Comment

ID: 8170890
it's taking forever do you use msn? maybe it'll send faster?
0

Author Comment

ID: 8170900
actually nm let me look at it =)
0

LVL 2

Accepted Solution

Jacamar earned 100 total points
ID: 8170907
yeah I have msn
0

Author Comment

ID: 8170915
it worked thank you so so much.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no oâ€¦
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most pâ€¦
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applicâ€¦
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can launâ€¦
###### Suggested Courses
Course of the Month14 days, 19 hours left to enroll

#### 578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.