0

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
2
  • In the loop, your p,r,R,t everything is constant. How can anything chnage. Do p=p+totalInt and total=p Commented Jul 11, 2014 at 7:35
  • I don't think you should be using float (if not necessary anyway), use double instead. But for your case use BigDecimal anyway since for finance the double gives non accurate results. Commented Jul 11, 2014 at 7:35

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

I am assuming the question is about the output formating

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

2 Comments

It's best to read the whole question, you can see that his total interest and balance are also wrong.
ye ye you are right. I'm sorry. Didn't see his results were wrong.I thought it was only about formatting. Anyway its answered already :)
0

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

4 Comments

thanks a lot but the interest and the total interest is the same when I executed.
@user3636934 he needs to keep the totalInterest outside of the loop, not the interest user3828257 that's because as I explained in my answer you need to move totalInt outside of your loop, and need to increment your total with the interest.
@SimonVerhoeven by totalInt you mean the declaration or the whole "totalInt= (float) interest;" and the one I need to increment is the total am I right?
@user3828257 yes, currently you're overriding your totalint every time in the loop. You need to increment it. Just do totalInt += interest in the loop. I clarified my answer a bit more for you.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.