- Course
- Functions B
- Function to calculate the power of a number
Function to calculate the power of a number
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a C# program that implements a function called Power and calculate the result of raising an integer to another number.
Input
2
3
Output
8
Solution
using System;
public class FunctionCalculatePowerNumber
{
public static void Main(string[] args)
{
int number = Convert.ToInt32(Console.ReadLine());
int exponent = Convert.ToInt32(Console.ReadLine());
int power = Power(number, exponent);
Console.WriteLine(power);
}
public static int Power(int number, int exponent)
{
int result = 1;
for (int i = 0; i < exponent; i++)
{
result *= number;
}
return result;
}
}