Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

My C program has to contain a loop that increases the interest rate, and a nested loop that calculates the interest and displays a total per each interest rate. Can anyone point me in the right direction?

The program should output 10 interest rates and the corresponding total interest plus the loan

The interest rate starts .01 (1 percent) and should end at .10 (10 percent).

#include <stdio.h>

int main(void)
{
    double loan = 1000.00, rate = 0.01, output;
    int year = 1;

    do {
        rate + 0.01;

        for (year = 1; year < 11; year++) {

            output = rate * loan + loan;

        }

        printf("%d   $%d\n", year, output);

    } while (year < 11);

    return 0;
}
share|improve this question
    
rate + 0.01 does nothing useful – Jean-François Fabre 1 hour ago
    
Does the user have to input anything or not ? I am not finding it clear to understand what you actually want the program to do. What should the output look like ? – Rizzo 1 hour ago
    
The user does not input anything. The program should output a table display the rate and what the interest is at that rate. So it should out 10 rows of interest and the corresponding total at that interest rate – dreed 1 hour ago
    
Ok, for starters, the printf statement should be inside the for loop. Also, check out the good answer by @Schwern – Rizzo 1 hour ago

First things first, turn on warnings. C compilers normally don't give you warnings, it's generally -Wall to get the basics (not all). Once we do that...

test.c:17:36: warning: format specifies type 'int' but the argument has type 'double' [-Wformat]
        printf("%d   $%d\n", year, output);
                      ~~           ^~~~~~
                      %f

That tells us you're passing a floating point number to printf but trying to treat it as an integer, so it's going to be translated to a string incorrectly. It needs to be.

        printf("%d   $%.2f\n", year, output);

%f for a floating point number, and the .2 means to only use two decimal places like money.


test.c:9:14: warning: expression result unused [-Wunused-value]
        rate + 0.01;
        ~~~~ ^ ~~~~
2 warnings generated.

This one tells us that the rate is never incremented. It's added to, but never stored anywhere. Instead you want rate += 0.01.


Now it runs. Once. And it only reports on year 11.

The problem is your outer do/while loop is using year, but the for loop will end with the year at the threshhold, so it will immediately stop. Instead of using a do/while loop, which is rarely necessary, use another for loop.

Second problem is you're printing the output and year outside the year for loop. So you'll only ever get the result for the last year. If you want all the years, the print goes inside.

#include <stdio.h>

int main(void)
{
    const double initial_loan = 1000.00;

    for( double rate = 0.01; rate <= 0.10; rate += 0.01 ) {
        double loan = initial_loan;

        for (int year = 1; year <= 10; year++) {
            loan += rate*loan;
            printf("%d @ %.2f%% $%.2f\n", year, rate * 100, loan);
        }
    }

    return 0;
}

Notice that I declare variables in the narrowest possible scope. This tells you when they're appropriate to be used, and avoids goofs like trying to use the year in the outer loop.

A lot of C material will be extremely conservative and tell you to declare all variables at the start of a function. This isn't necessary, and hasn't been for a long, long time. Though declaring variables in a for loop is a C99 thing, that part of C99 is very well supported.

share|improve this answer
1  
Thanks, you explained everything clearly. I had written this program multiple ways but I think my main error was passing the float as int when trying to use printf. – dreed 41 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.