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;twos<=10;twos++)
        printf("2^%.0f = %.0f\n",twos,pow(2,twos));
    return(0);
}

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.

* 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.

* Here is the sample output, which is more-or-less what you were trying to achieve to answer the exercise properly: