Powers and Roots
Use pow, sqrt, and friends.
Powers and Roots
Two of the most useful tools in math.h are pow for raising a number to a power, and sqrt for finding a square root.
Both work with double values and return a double result.
The pow Function
pow(base, exponent) computes base raised to the exponent.
For example pow(2, 3) means 2 x 2 x 2, which is 8.
#include <stdio.h>
#include <math.h>
int main(void) {
double result = pow(2.0, 3.0);
printf("2^3 = %.0f\n", result);
return 0;
}All lessons in this course
- Including math.h
- Powers and Roots
- Trig and Logarithms
- Rounding and Absolute Values