Showing posts with label prime number in visual basic net. Show all posts
Showing posts with label prime number in visual basic net. Show all posts

Sunday, April 9, 2017

Prime Number in Visual Basic NET

Here is a sample program that I wrote using Visual Basic NET to ask the user to give a number and then our program will check and determine if the given number is a prime number or not a prime number. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.





Sample Program Output


Program Listing

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim a, b As Integer
        Dim t As Boolean
        a = Val(TextBox1.Text)
        t = True
        For b = 2 To (a - 1)
            If a Mod b = 0 Then
                t = False
                Exit For
            End If
        Next b
        If t Then
            Label2.Text = "The number " & a & " is a PRIME Number."
        Else
            Label2.Text = "The number " & a & " is NOT a PRIME Number."
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        End
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        TextBox1.Text = ""
        Label2.Text = ""
        TextBox1.Focus()
    End Sub
End Class


Monday, June 2, 2014

Prime Number Checker in Visual Basic NET

One of the fascinating machine man was able to develop and invented is the computer. This tool makes our day to day work more easier and efficient in a way that most common task and repetitive work can be done by the computer  faster and most important is its accuracy to perform computation.  Computer evolves a lot from a mere tool for solving mathematical problems during second world war into a machine that runs our entire information system around the world.  As a user we always rely on the aspect the computers can always gives us correct results as long the program that runs on it is correct all the time.

This is also one of my thinking before when I started my study in computer science during my college days our teacher would tell us if we right correct code in our program it will always generate correct results he is correct. But I have later learned in my later career in information technology field that even we have a correct written program there are some situation that our program will misbehave or gives us wrong results. It is a funny story that I have read and hear that some people always blame the computer if they can't get the right result that they needed. Common sense tells us that computer is a machine it does not its own mind of thinking compared to us human being that we are intelligent over many things including animals that surround us.

Computer scientist wrote different principles in programming to create error free programs but the problem is the users does not understand most of the time what are the limitations of the computer.  For instance our program will ask the user to enter their name in the system and our program will store those information in the database. If the user will give erroneous information about themselves or wrong spelling our program does not have the capability to check for those entries in the first place. We can only check if the fields are not empty before it will be submitted to our system to be saved but the verification must be done first by the user in the first place.

For me the biggest time that is allocated is not just writing the code of the program but in the process of debugging finding and correcting errors in our program. Once we correct the errors in our program we deploy our program to our end user they are the one will scrutinize and give us the feedback about the problems that they encounter in your our program. In order for us to minimize this problem we must able to have a good program planning before we write our first line of code in our program. By this way we can focus in solving the main problem of program and make our user happy by giving them a program that is very easy to use and more efficient to cater their day to day work.

In this article I will discuss about a program that will check if the number being given by the user is prime number or not.  Most of us during our elementary years our math teacher teach us the if a number the is integer by nature is bigger than one and have no other divisors other than oen and itself is considered as prime number.  For example 5 is a prime number, since no number except 1 and 5 divides on it.  It is also the same with 8 is not considered as prime number because it can be divide by 2 and 4.

What our program will do is very simple it will ask the user to enter a number in our text box. There are three command button the first command button is check prime if the user click this button it automatically determines whether the number is prime number or not a prime number by displaying a message in our form.  The second command button is the clear is this clear button allows the user to clear the text box and label message in our form that enables the user to enter new number to be check by our program. The third and last button is quit button this quit button if the user choose it will display a message dialog box asking the user if the user wants to quit the program for good or continue using the program.  I also put a code that will check if the user was not able to put any number in the text box it will give the user a message to put some numbers in the text box. This code I wrote just to protect our program to generate a wrong value.


I hope you have learned something in our program Prime Number Checker written in Visual Basic .NET if you have some questions, comments and suggestions how I can improve my codes and articles please send me an email I am very glad to hear it from you as readers in this website.

Thank you very much.




Sample Output of Our Program

Program Listing

Private Sub Prime_Number_Check(ByVal number_value As Integer)
        Dim prime_number As Boolean = True
        For i = 2 To number_value / 2
            If (number_value Mod i = 0) Then
                prime_number = False
                Exit For
            End If
        Next i
        If prime_number = False Then
            lbl_message.Text = "The number " & Val(txt_value.Text) & " is not a Prime number."
        Else
            lbl_message.Text = "The number " & Val(txt_value.Text) & " is a Prime number."
        End If
    End Sub
    Private Sub btn_check_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_check.Click
        If Trim(txt_value.Text) = "" Then
            MsgBox("You must put a number it can't be empty.")
            Me.txt_value.Focus()
            Me.lbl_message.Text = ""
        Else
            Prime_Number_Check(Val(txt_value.Text))
        End If

    End Sub
    Friend WithEvents Button1 As System.Windows.Forms.Button

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.txt_value.Text = ""
        Me.lbl_message.Text = " "
        Me.txt_value.Focus()
    End Sub
    Friend WithEvents btn_quit As System.Windows.Forms.Button

    Private Sub btn_quit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_quit.Click
        If MsgBox("Are you sure you want to quit?", MsgBoxStyle.Critical + MsgBoxStyle.YesNo, "Quit Program?") = MsgBoxResult.Yes Then
            Me.Hide()
        Else
            Me.Show()
            Me.txt_value.Text = ""
            Me.lbl_message.Text = " "
            Me.txt_value.Focus()
        End If
    End Sub
End Class