# VB: Square and Square Root of Given Number Using Radio Button

**Program Abstraction:**

*This visual basic program shows how to find square and square root of a given number using two radio buttons.*

This is a simple Visual Basic program to find square and square root of a number using radio buttons. Here a textbox accept a number and a result text shows the resulting value according to selection of radio buttons whether square or square root.

Here Textbox1 accept a number and user has to select a radio button whether square or square root. Then Get Result button will give you a result on the Label1 according to the radio button selection. If the radio button1 (square) is selected, then Label1 text shows the square of the textbox1 number. If the button selection is square root, then Label1 text will be the square root of the given number.

**See the picture: **

**Background code:**

Here only button has an event of click, which perform all works:

Dim num, squar As Integer

Dim sqroot As Double

num = CInt(TextBox1.Text)

If RadioButton1.Checked Then

squar = num * num

Label1.Text = squar

ElseIf RadioButton2.Checked Then

sqroot = Math.Sqrt(num)

Label1.Text = sqroot

End If

**Code Explanation: **

Dim num, squar As Integer

Dim sqroot As Double

This is the code for declaring variable num and square as integer. Variable sqroot should be ‘double’ to represent rational values.

____________________________________________

num = CInt(TextBox1.Text)

the variable num get the value of Textbox1 after convertin to Integer. CInt means Converting into Integer.

____________________________________________

If RadioButton1.Checked Then

squar = num * num

Here, button action check the radio button selection, if the Radio button 1 (reperesents square) then the variable ‘square’ get the value of num*num (that is square).

____________________________________________

Label1.Text = squar

Here Lable1 text is replace with the value of variable square.

____________________________________________

ElseIf RadioButton2.Checked Then

sqroot = Math.Sqrt(num)

Label1.Text = sqroot

End If

This code works only if the radio button 2 is selected. Here variable sqroot get the square root of the number. To get square root, the class Math is necessary. Math.sqrt is needed to get the square root.

____________________________________________

If we want getting result as message box, then replace Label1.Text = sqroot code with MessageBox.Show(squar)

Here is the **sample output: **

## Be First to Comment