Solution for Exercise 11-12

ex1112

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

int main()
{
    float twos;

    puts("The Holy Numbers of Computing");
    for(twos=0.0;twos<=10.0;twos++)
        printf("2^%.0f = %.0f\n",twos,pow(2.0,twos));
    return(0);
}

Output

The Holy Numbers of Computing
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128
2^8 = 256
2^9 = 512
2^10 = 1024

Notes

* Many other possibilities exist to get this code correct, so your answer most definitely doesn't have to match mine.

* The pow() function eats floating point values, which is why variable twos is declared as a float.

* Other programming languages use special operators (symbols) to express power. Not in C, where the pow() function is used.

* Yes, you can use the ++ operator on a floating-point value.

* I hope the compiler didn't notice that you forget the second right paren at the end of the printf() statement.

* Some compilers forgive you when you use integer literals in the code, such as 2 instead of 2.0.

* The pow() function is used in Line 10 to calculate the power of 2. It's used immediately, although you don't have to have it set that way in your own code, i.e., you could save the value in a variable and then use the variable.

* The %.0f conversion character limits output of a floating point value to only numbers on the left side of the decimal.