Tuesday, May 5, 2009

Prime Number Tester

Prime numbers are easy to determined when they are less than 100. However, once they are more than 100, it maybe difficult to determine. Fortunately, in Visual Basic, you can write a program to test the numbers.

In this program, I use the Select Case ......End Select statement to determine whether a number entered by a user is a prime number or not. For case 1, all numbers that are less than 2 are prime. In Case 2, if the number is 2, it is a prime number. In the last case, if the number N is more than 2, I need to divide this number by all the numbers from 3,4,5,6,........up to N-1, if it can be divided by any of these numbers, it is not a prime number, otherwise it is a prime number. I use a Do......Loop While statement to control the program flow. Here I also used a tag="Not Prime' to identify the number that is not prime, so that when the routine exits the loop, the label will display the correct answer.

Please refer to the link below for the source code.

http://www.vbtutor.net/VB_Sample/Prime.htm


No comments: