pow() raises one floating-point value to another power. Both arguments and the return value are double.
double pow(double base, double exponent);
#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
#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.
#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;
}
#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;
}
#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.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.