user3828257
user3828257

Reputation: 3

Use output as an input on the next loop

I need to have an outcome like this:
example the user input are principal = 2000, term=6, rate=2%

Period Interest  Total Interest  Total Balanced<br>
    1      6.66            6.66             2006.60 
    2      6.69            13.35           2013.35 
    3      6.71            20.06           2020.06 
    4      6.74            26.80           2026.80 
    5       6.75           33.55           2033.55 
    6       6.78           40.33           2040.33

My code is:

import java.io.*;
public class interest
{
    public static void main(String[] args)throws Exception
    {
        BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in));

        float p, t, r, total;
        int a = 1;

        System.out.println("Enter the amount deposited: ");

        p = Integer.parseInt(bufferedreader.readLine());

        System.out.println("Enter the number of term: ");

        t = Integer.parseInt(bufferedreader.readLine());

        System.out.println("Enter the interest rate(%): ");

        r = Integer.parseInt(bufferedreader.readLine());

        System.out.println("Period \t Interest  Total Interest  Total Balance");

        while ( a &lt;= t)
        {
            System.out.print("   " + a + "\t   ");
            a++;

            {
                float R = (float) (r/100)/t;
                float interest = (float) p * R;
                float totalInt = (float) interest ;
                total = p + totalInt;

                System.out.println(interest + "\t\t" + totalInt + "\t      " + total);
            }
        }
    }
}

but the outcome turns up like this:

Period   Interest  Total Interest  Total Balance
   1       6.6666665        6.6666665         2006.6666
   2       6.6666665        6.6666665         2006.6666
   3       6.6666665        6.6666665         2006.6666
   4       6.6666665        6.6666665         2006.6666
   5       6.6666665        6.6666665         2006.6666
   6       6.6666665        6.6666665         2006.6666

Upvotes: 0

Views: 142

Answers (4)

Simon Verhoeven
Simon Verhoeven

Reputation: 1323

Move your totalInt declaration outside of your while declaration. You're currently resetting it in every loop, thus it's not actually your total interest but your current interest. You need to do: totalInt += interest; in the loop. And you don't need to cast interest to a float again for the increment, as it's already declared as a float. Also it might be cleaner to do total += interest rather than starting out from your base deposit and incrementing it with your totalInt every time.

And as to your last issue, the formatting, just do something along the lines of:

System.out.printf("%.2f \t\t %.2f \t\t %.2f\n", interest, totalInt, total);   

Or take a look at DecimalFormat

Upvotes: 1

Carl di Ortus
Carl di Ortus

Reputation: 177

I think what you want is, change the first line to:

float p, t, r, total, R, interest, totalInt;

and remove float declaration inside loop

Upvotes: 0

user3636934
user3636934

Reputation: 311

Hi keep float interest before while loop like this

float interest=0;

while ( a <= t)
{

System.out.print(" " + a + "\t ");

a++;

{

float R = (float) (r/100)/t;

interest = interest + (float) p * R;

float totalInt = (float) interest ;

total = p + totalInt;

System.out.println(interest + "\t\t" + totalInt + "\t " + total);
}

 }

 }

Now Execute it you will get the required output please like if you are satisfied.

Thank You

Upvotes: 0

gkrls
gkrls

Reputation: 2664

I am assuming the question is about the output formating

System.out.printf("%.2f \t\t %.2f \t\t %.2f\n", interest, totalInt, total);   

Upvotes: 0

Related Questions