Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Cách tính giá trị lũy thừa (power exponent) trong C#

Để tính giá trị lũy thừa, dùng phương thức Math.Pow().

Ở đây n là số cơ số, p là số mũ:

double res = Math.Pow(n, p);

Code hoàn chỉnh:

Ví dụ

using System; 
class Program { 
    static void Main() { 
        double n, p; 
        n = 7; 
        p = 3; 
        Console.WriteLine("Exponent Power= " + n); 
        double res = Math.Pow(n, p); 
        Console.WriteLine("Result= {0}", res); 
        Console.ReadLine(); 
    } 
}

Kết quả

Exponent Power= 7
Result= 343