0

I am getting a slightly different result from what asked in the pset. In fact my code will give as a result $107374184.0000 instead of $10737418.23, but I don't understand where is the problem. Thanks ahed for the help!

    #include <stdio.h>

    int main(void)
    {
        printf("Days in month: 30\n");
        printf("Pennies on first day: 1\n");
        float p = 0.1;
        for (float d = 0; d < 30; d++)
        {
            p *= 2;
        }
        printf("$%f\n", p);
    }
0

For starters, a penny is 0.01, not 0.1. Next, what is the maximum number of significant digits that can be stored in a float? What about a double?

As a side note, it's generally a bad practice to use a float as a simple counter to control a for loop. Unless there's a good reason for using a float, an int should be used instead.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .