Question

I have a constructor that returns the result of two ints with a 'to the power of' operator. I am trying to use the math.pow method to do this but im not to sure how it work.

Here is my constructor

public int Power (int firstNumber, int secondNumber)
{
    result = Math.Pow(firstNumber, secondNumber);
    return result;
}

and the code that calls it

case "^":
    result = application.Power(firstInt, secondInt);
    labelResult.Text = ("" + result);
    break;

How do i use the Math.Pow method in this situation?

Was it helpful?

Solution

As far as the error you're getting:

Math.Pow takes 2 doubles as arguments and returns a double. Your method is declared as returning an int. This means that result is an int. You need to cast the result of Math.Pow to int:

result = (int)Math.Pow(firstNumber, secondNumber);

OTHER TIPS

Basically, it returns a specified number raised to the specified power.

The first argument contains the value you want to use

The second argument contains the power value you want to implement.

For example, if you have this

result = Math.Pow(3, 3);
return result;

It will return value 27 because it is 3 x 3 x 3.

But if I write

result = Math.Pow(1, 3);
return result;

It will return value of 1 because it is 1 x 1 x 1.

What if I write the 1st argument of 5 and 2nd argument 1?

result = Math.Pow(5, 1);
return result

It will return as 5 because it is 5 x 1.

You can figure it your own. http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_pow

UPDATED

Here's the example of the input. Use Power instead of declaring application.Power

case "^":
    result = Power(firstInt, secondInt);
    labelResult.Text = ("" + result);
    break;

Like Minelava said, the Math.Pow method has two parameters, the first parameter indicates the number you are trying to base the result on. The second parameter is the exponent itself.

Math.Pow(3, 2) = 3 * 3 = 9

Math.Pow(2, 3) = 2 * 2 * 2 = 8

If you like, you could directly call the Math.Pow method, so you do not need Power method anymore. I think it's more simple otherwise you have another concern to have other Power method.

case "^":
    result = Math.Pow(firstInt, secondInt);
    labelResult.Text = ("" + result);
    break;

Just my two cents. Thanks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top