Home >Backend Development >C#.Net Tutorial >How to write x raised to the third power in C language
There are two ways to calculate the cube of x in C language: use the pow() function and use loop operation
In How to calculate the cube of x in C language?
In C language, there are two main methods to calculate the cube of x:
1. Use the pow() function:
<code class="c">#include <math.h> int main() { double x = 5.0; double result = pow(x, 3); printf("%f 的三次方是 %f\n", x, result); return 0; }</code>
Output:
<code>5.0 的三次方是 125.000000</code>
2. Use loop operation:
<code class="c">int main() { int x = 5; int result = 1; for (int i = 0; i < 3; i++) { result *= x; } printf("%d 的三次方是 %d\n", x, result); return 0; }</code>
Output:
<code>5 的三次方是 125</code>
The above is the detailed content of How to write x raised to the third power in C language. For more information, please follow other related articles on the PHP Chinese website!