pow() in C: Powers and Exponents

pow() raises one floating-point value to another power. Both arguments and the return value are double.

Syntax Top ↑

double pow(double base, double exponent);

Square and Cube Top ↑

#include <math.h>
#include <stdio.h>

int main(void) {
    double value = 5.0;

    printf("Square: %.0f\n", pow(value, 2.0));
    printf("Cube  : %.0f\n", pow(value, 3.0));

    return 0;
}

Output

Square: 25
Cube  : 125

Fractional Exponent Top ↑

#include <math.h>
#include <stdio.h>

int main(void) {
    printf("%.2f\n", pow(81.0, 0.5));
    return 0;
}

Output

9.00

For a simple square root, sqrt() communicates the intent more directly.

Preserved pow() Variations Top ↑

Negative Exponent

#include <math.h>
#include <stdio.h>

int main(void) {
    double base = 2.0;
    double exponent = -3.0;

    printf("%.2f raised to %.2f = %.5f\n",
           base, exponent, pow(base, exponent));

    return 0;
}

User-Entered Base and Exponent

#include <math.h>
#include <stdio.h>

int main(void) {
    double base;
    double exponent;

    printf("Enter base and exponent: ");
    if (scanf("%lf %lf", &base, &exponent) != 2) {
        printf("Invalid input.\n");
        return 1;
    }

    printf("Result = %.6f\n", pow(base, exponent));
    return 0;
}

Integer Power with a Loop

#include <stdio.h>

long long integer_power(int base, unsigned int exponent) {
    long long result = 1;

    for (unsigned int i = 0; i < exponent; i++) {
        result *= base;
    }

    return result;
}

int main(void) {
    int base = 3;
    unsigned int exponent = 4;

    printf("%d raised to %u = %lld\n",
           base, exponent, integer_power(base, exponent));

    return 0;
}

This loop version is for non-negative integer exponents. pow() remains appropriate for floating-point and fractional exponents.





plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer