0Pricing
C Academy · Lesson

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

  1. Including math.h
  2. Powers and Roots
  3. Trig and Logarithms
  4. Rounding and Absolute Values
← Back to C Academy