+ 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#

10th Nov 2022, 4:20 AM
IDOWU Olayinka
IDOWU Olayinka - avatar
7 Answers
+ 6
Math.Pow(x, y)
10th Nov 2022, 5:23 AM
Tibor Santa
Tibor Santa - avatar
+ 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;
10th Nov 2022, 8:14 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Is it getting wrong result? Or Error in code? Add details.. Show the code snippet pls..
10th Nov 2022, 7:34 AM
Jayakrishna šŸ‡®šŸ‡³
+ 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
10th Nov 2022, 7:32 PM
Jayakrishna šŸ‡®šŸ‡³
+ 1
Ive done that but still didnt give correct answer
10th Nov 2022, 7:31 AM
IDOWU Olayinka
IDOWU Olayinka - avatar
+ 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); } } }
10th Nov 2022, 7:37 AM
IDOWU Olayinka
IDOWU Olayinka - avatar
+ 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); } } }
10th Nov 2022, 7:39 AM
IDOWU Olayinka
IDOWU Olayinka - avatar