0
How come the result is 36 and 81? Explain
static int Pow(int x, int y=2) { int result = 1; for (int i = 0; i < y; i++) { result *= x; } return result; } static void Main(string[] args) { Console.WriteLine(Pow(6)); Console.WriteLine(Pow(3, 4)); }
1 Respuesta
+ 4
Pow (6) uses the default value for y of 2. So is the same as Pow (6,2), or 6 squared. This is the same as saying 6*6, so that's 36
Then Pow (3,4) uses the value passed for y of 4. So that is 3 to the power of 4 or 3*3*3*3. That's 81.