Find a square root of a given number

Web Design and Digital Marketing

Write a program to find a square root of a given number from a text input box and show the answer in a label.

NOTE: To solve this problem we will be using VB.NET as our programming language. You will also need visual studio IDE to follow along.

SOLUTION:

1. Design the GUI

Textbox, label and button
Give your controls meaningful names.

Inputbox – numberTextbox
Squareroot Button – sqrtButton
Answer label – ansLabel

Now that all boxes are labelled.

UI

Watch Video and follow along.

2. Write Code

This is the only code you need to find a square root of a given number.

Private Sub sqrtButton_Click(sender As Object, e As EventArgs) Handles sqrtButton.Click
        Dim number As Int64
        number = numberTextBox.Text
        ansLabel.Text = Math.Sqrt(number)
End Sub

Explanation.

Firstly, we are defining a number and giving it a value of our input textbox (this is optional, I just did it for simplicity in my code).
To find a square root of a number in VB.NET we call a Math prebuilt Sqrt function and pass in the number we need to find its square root in parenthesis.
Finally, we call our answer label and assign it to the answer.

3. Run and Test

Now that your UI and code is done its time to test our program.

UI run

Conclusion

I deliberately left some of the calculations for you to work on them before I post the next post. Finding a square of a given number is very easy as you have seen. Always remember to comment, like share and subscribe. 

Leave a Comment

Your email address will not be published.