ในการคำนวณค่าเลขชี้กำลัง ให้ใช้เมธอด Math.pow()
โดยที่ n คือตัวเลขและ p คือกำลัง -
double res = Math.Pow(n, p);
ต่อไปนี้เป็นรหัสที่สมบูรณ์ -
ตัวอย่าง
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();
}
} ผลลัพธ์
Exponent Power= 7 Result= 343