In C language, there are two ways to express 10 to the nth power: use the pow() function, accept the base and exponent, and return the exponent of the base. Use the shift operator (<<) to shift 1 to the left by the exponent number of places to calculate 10 raised to the power.
How to express 10 to the nth power in C language?
In C language, there are two ways to express 10 to the nth power:
Method 1: Use the pow() function
<code class="c">#include <math.h> int main() { int n = 5; double result = pow(10, n); printf("10 的 %d 次方是 %lf\n", n, result); return 0; } </p> <p><strong>Method 2: Use the bit shift operator (<<)</strong></p> <pre class="brush:php;toolbar:false"><code class="c">int main() { int n = 5; int result = 1 << n; printf("10 的 %d 次方是 %d\n", n, result); return 0; }</code>
Instructions:
Note:
n
is a non-negative integer. n
is too large, the pow()
function may return inf
(positive infinity) or nan
( non-numeric). n
, the bit-shift operator may be more efficient than the pow()
function. The above is the detailed content of How to express 10 to the nth power in C language. For more information, please follow other related articles on the PHP Chinese website!