+ 2
C# problem
Hello guys Iām working on a project and I need help How do I raise a number to its power in C#? In python we use ** but I donāt know of C#
7 Answers
+ 6
Math.Pow(x, y)
+ 5
Math.Pow gives you a double value, you cannot store it in an int variable.
double radsq = Math.Pow(rad, 2);
double area = pi * radsq;
But your original approach is also correct, raising to the second power is the same as multiplying by itself.
double area = pi * rad * rad;
+ 2
Is it getting wrong result? Or Error in code? Add details..
Show the code snippet pls..
+ 2
For input 5:
First :
rad = 5*2 = 10;
area = 3.14 * 10 = 31.4
// use rad*=rad; instead..
Second :
Math.Pow(5, 2) => 25.0 store in double type
area = 3.14 * 25.0 = 78.5
+ 1
Ive done that but still didnt give correct answer
+ 1
Im to get the area of a circle after asking the user for radius. Now hereās my code, please help me review
static void Main(string[] args)
{
const double pi = 3.14;
double radius;
//your code goes here
int rad = Convert.ToInt32(Console.ReadLine());
rad *= 2;
double area = pi * rad;
Console.WriteLine(area);
}
}
}
+ 1
Adding Math.Pow still gives me error
static void Main(string[] args)
{
const double pi = 3.14;
double radius;
//your code goes here
int rad = Convert.ToInt32(Console.ReadLine());
rad = Math.Pow(rad, 2);
double area = pi * rad;
Console.WriteLine(area);
}
}
}