- 3
I want someone to explain to me step by step
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { 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 Answer
+ 1
static int Pow(int x, int y=2) // y=2 here 2 default argument value for y. If you don't pass value as parameter in calling statement then y value taken as 2
{
// loop runs from I=0 to you so y times it runs and find result = 1*x*x*x*...*x ( y times)
int result = 1;
for (int i = 0; i < y; i++)
{
result *= x;
}
return result;
}
static void Main(string[] args)
{
Console.WriteLine(Pow(6)); // passing x value as 6 but no y value so y value is 2 default.
It returned value = 1*6*6=36
Console.WriteLine(Pow(3, 4)); // here passing both values so x=3, y = 4. It gets return value as 1*3*3*3*3= 81
1 is default value for result variable.
Hope it helps...