Write a VB program to find out twin prime numbers between 10 to 100. Twin primes are defined to be two consecutive odd numbers, which are prime (Accept input through textbox and display result on form)
Answers
Answered by
3
Write a VB program to find out twin prime numbers between 10 to 100. Twin primes are defined to be two consecutive odd numbers, which are prime
Explanation:
A Twin prime are the numbers with following properties:
- Numbers must be prime
- There must be difference of two among the two prime numbers.
Example:
(3,5) , (5,7) , (11,13) and (17,19)
Program
Dim n1 As Integer
Dim n2 As Integer
Dim i As Integer
Dim flag As Boolean
Private Sub Command1_Click()
flag = False
n1 = Val(Text1.Text)
n2 = Val(Text2.Text)
If n1 > 10 And n2 < 100 Then
While n1 <= n2
For i = 2 To n1 \ 2
If n1 Mod i = 0 Then
flag = True
End If
Next
If flag = True Then
MsgBox ("Either of the number is not prime")
End
Else
cnt = cnt + 1
n3 = n1
n1 = n1 + 2
flag = False
End If
If cnt = 2 Then
If n3 = n2 Then
MsgBox "No1 and no2 are twin primes"
Else
MsgBox "No1 and no2 are not twin primes"
End If
End
End If
Wend
End If
End Sub
Private Sub Text1_Click()
Text1.Text = ""
End Sub
Private Sub Text2_Click()
Text2.Text = ""
End Sub
Similar questions