Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

For Loop question

  • 12-11-2008 7: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,212 ✭✭✭✭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