0Pricing
C Academy · Lesson

Powers and Roots

Use pow, sqrt, and friends.

Powers and Roots is a free C Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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;
}

Squares with pow

To square a number, raise it to the power of 2.

Here we compute the area of a square room by squaring its side length.

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

int main(void) {
    double side = 5.0;
    double area = pow(side, 2.0);
    printf("area = %.0f\n", area);
    return 0;
}

Fractional Exponents

pow also accepts fractional exponents. Raising to the power 0.5 is the same as taking a square root.

So pow(9.0, 0.5) equals 3.0.

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

int main(void) {
    double r = pow(9.0, 0.5);
    printf("9^0.5 = %.1f\n", r);
    return 0;
}

The sqrt Function

sqrt(x) returns the square root of x: the number that, multiplied by itself, gives x.

It is clearer and faster than using pow(x, 0.5) when you just need a square root.

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

int main(void) {
    double r = sqrt(144.0);
    printf("sqrt(144) = %.0f\n", r);
    return 0;
}

Negative Inputs to sqrt

The square root of a negative number is not a real number.

Calling sqrt(-1.0) returns a special value called NaN (Not a Number). Always check that your input is zero or positive.

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

int main(void) {
    double r = sqrt(-4.0);
    printf("result = %f\n", r);
    return 0;
}

Cube Roots with cbrt

For cube roots, math.h offers cbrt.

Unlike sqrt, cbrt works fine with negative numbers, since every real number has a real cube root.

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

int main(void) {
    printf("%.0f\n", cbrt(27.0));
    printf("%.0f\n", cbrt(-8.0));
    return 0;
}

The Pythagorean Theorem

Powers and roots combine nicely in geometry. The length of a right triangle's longest side is the square root of the sum of the squares of the other two sides.

Here we compute the hypotenuse of a 3-4-5 triangle.

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

int main(void) {
    double a = 3.0, b = 4.0;
    double c = sqrt(pow(a, 2.0) + pow(b, 2.0));
    printf("hypotenuse = %.1f\n", c);
    return 0;
}

The hypot Helper

That last pattern is so common that math.h includes hypot(a, b), which directly returns the hypotenuse.

It is more accurate than doing the squares and root by hand for very large or small numbers.

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

int main(void) {
    double c = hypot(3.0, 4.0);
    printf("hypot = %.1f\n", c);
    return 0;
}

Choosing the Right Tool

Quick guide:

  • Square root: use sqrt
  • Cube root: use cbrt
  • Any power: use pow
  • Hypotenuse: use hypot

Pick the specific function when one exists. It is clearer and often more precise.

Practice Program

This program raises a number to a power, then takes the square root of the result, showing how the functions chain together.

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

int main(void) {
    double p = pow(10.0, 4.0);
    double r = sqrt(p);
    printf("%.0f -> %.0f\n", p, r);
    return 0;
}

Quick Check

Think about what each function returns.

Recap

You learned to raise numbers to powers with pow, find square roots with sqrt, cube roots with cbrt, and hypotenuses with hypot.

Remember that sqrt of a negative number gives NaN. Next we look at trigonometry and logarithms.

Frequently asked questions

Is the “Powers and Roots” lesson free?

Yes — the full text of “Powers and Roots” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.

What will I learn in “Powers and Roots”?

Use pow, sqrt, and friends. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Powers and Roots” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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