Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

For Loop question

  • 12-11-2008 07:58PM
    #1
    Closed Accounts Posts: 2


    // Loop: For n=1 to theNumberOfYears
    // interestEarned equals principal times interest rate
    // Accumulate the interest earned so far
    // new balance equals previous balance plus interestEarned
    // Print Out: Year# Interest New Balance
    // End of Loop

    can anyone write a sample code for this to help me please?
    If this isn't enough information, let me know. I'll try to give more info.


Comments

  • Registered Users, Registered Users 2 Posts: 1,119 ✭✭✭Donald-Duck


    for(n = 1; n <= numberOfYears; n++) {
    }

    For loops will look almost identical all the time. I didn't do the inside of the loop because its your homework.


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Also is it java or c/c++ ?


  • Registered Users, Registered Users 2 Posts: 23,202 ✭✭✭✭Tom Dunne


    Most of the seasoned programmers here can do that for you, but most won't until you show us what you have tried. And told us what language.

    We don't do homework. But we will help with it.


  • Closed Accounts Posts: 2 KRoNIC


    Tom Dunne wrote: »
    Most of the seasoned programmers here can do that for you, but most won't until you show us what you have tried. And told us what language.

    We don't do homework. But we will help with it.


    My apologies.

    Here is what I have so far.
    String y, interest, d;
    int years, i, dollars;
    double interestEarned, newBalance;

    (All the dataIn.readLine();
    Integer.parseInt(); etc etc
    ......



    for (int n=1; n<= years; n++) {
    interestEarned = (dollars * i);
    newBalance = (n + interestEarned);
    System.out.println( n + " " + (newBalance) + " dollars");
    } // end of loop

    It is compiling successfully and I know that it is doing the "(dollars * i)" calculation correctly, but from that point on I am confused as to what to do next. The next instruction after "interestEarned equals principal times interest rate" says "Accumulate the interest earned so far", then "new balance equals previous balance plus interestEarned" which I attempted but don't think I did correctly.


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    1. you don't need to put the sums in brackets.

    eg.

    interestEarned = dollars * i;

    2. n is basically a number. So n + interestEarned would be 1 + n + interestEarned, 2 + interestEarned, etc.

    What you want to do is

    newBalance += interestEarned;


    3. Be aware that you are using ints. They do not have floating points.


  • Advertisement
Advertisement