Mid From PDF Coding C# Coding Interview

Calculate power of a number without built-in pow()?

public double Power(double x, int n) {
if (n == 0) return 1;
double half = Power(x, n / 2);
if (n % 2 == 0)
return half * half;

else

return n > 0 ? x * half * half : (half * half) / x;
}

Explanation:

Uses fast exponentiation (divide and conquer) to calculate x^n in O(log n).

More from C# Programming Tutorial

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details