Title: 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.
Download the example to experiment with it and to see additional details.
|