Home >Backend Development >C#.Net Tutorial >How to write x raised to the third power in C language

How to write x raised to the third power in C language

下次还敢
下次还敢Original
2024-05-02 16:21:15630browse

There are two ways to calculate the cube of x in C language: use the pow() function and use loop operation

How to write x raised to the third power in C language

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn