Programming Course in C# ¡Free!

Function Power, local variables

 Saturday, April 06, 2013 published by Exercises C#
Proposed exercise

Create a function named "Power" to calculate the result of raising an integer number to another (positive integer) number. It must return another integer number. For example. Power(2,3) should return 8.

Note: You MUST use a repetitive structure, such as "for " or "while", you cannot use Math.Pow.

Output



Solution


using System;
public class F_Power
{
public static void Main()
{
Console.Write("Base: ");
int number = Convert.ToInt32( Console.ReadLine() );

Console.Write("Exponent: ");
int exponent = Convert.ToInt32( Console.ReadLine() );

Console.WriteLine("{0} ^ {1} = {2}",number, 
exponent, Power(number, exponent));
}

public static int Power(int number, int exponent)
{
int result = 1;

for(int i = 0; i < exponent; i++)
result *= number;

return result;
}
}