作业帮 > 综合 > 作业

vb编程 输入一个数看看是不是质数,要用IsPrime这个方程

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/21 09:01:14
vb编程 输入一个数看看是不是质数,要用IsPrime这个方程
Prime Numbers
A prime number is a number that can be evenly divided by only itself and 1.For
example,the number 5 is prime because it can be evenly divided by only 1 and 5.The
number 6,however,is not prime because it can be evenly divided by 1,2,3,and 6.
Write a Boolean function named IsPrime which takes an integer as an argument
and returns true if the argument is a prime number or false otherwise.Use the func-
tion in an application that lets the user enter a number and then displays a message
indicating whether the number is prime.
Private Function isPrime(intIndicated As Integer) As Boolean
If intIndicated >= 2 Then
isPrime = True
For i = 2 To intIndicated - 1
If intIndicated Mod i = 0 Then isPrime = False: Exit For
Next i
Else
isPrime = False
End If
End Function
Private Sub Command1_Click()
Dim intInput As Integer
intInput = InputBox("Please enter a integer :", "Enter a number")
If isPrime(intInput) Then
MsgBox intInput & " is a prime number."
Else
MsgBox intInput & " is NOT a prime number."
End If
End Sub
另外,你用的是VB6还是.NET,我上面是按VB6写的.