# Calculate Nth roots in C# To calculate Nth roots, you can simply use the formula:

`    root = K1/N`

The example program uses the following code to calculate roots.

```// Calculate the root.
private void btnCalculate_Click(object sender, EventArgs e)
{
double number = double.Parse(txtNumber.Text);
double root = double.Parse(txtRoot.Text);

// Calculate the root.
double result = Math.Pow(number, 1 / root);
txtResult.Text = result.ToString();

// Check the result.
double check = Math.Pow(result, root);
txtCheck.Text = check.ToString();
}```

The code parses the number and the root. It uses Math.Pow to raise the number to the 1/root power and displays the result. To check the result, the code then raises the result to the root power and displays the new result.    This entry was posted in mathematics and tagged , , , , , , , , , , , . Bookmark the permalink.

### 2 Responses to Calculate Nth roots in C#

1. TheGuyWhoDisliked says:

This uses the math features of C#. This does not do it recursively. Those are the reasons I disliked this.

2. Rod Stephens says:

The intent here was to just do the job, not to show how to calculate the number yourself. If you do want to do it yourself, you can use Newton’s method as shown in this example:

Use Newton’s method to find the roots of equations in C#

Simply find the roots of the equation Y = XN.

Or you can use subdivision. Pick an upper and lower bound, perhaps lower = 1 and upper = N / 2. Then check the value in the middle M. If MN > N, set lower = M and repeat. If MN < N, set upper = M and repeat.

Either way it still doesn’t need to be recursive, though.

This site uses Akismet to reduce spam. Learn how your comment data is processed.