0
//This program will calculate the compound interest. 

import java.io.*;
import java.text.*;

public class Program08
{
    public static void main (String args[])
    throws java.io.IOException 
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        DecimalFormat roundMyDecimal=new DecimalFormat("0.00");

        float amount;
        float principal;
        float interest;
        float compounds;
        float years;

        System.out.print("Enter the principal: ");                                  // inputs principal.
        principal=Float.parseFloat(br.readLine());

        System.out.print("Enter the number of years: ");                    // inputs years.
        years=Float.parseFloat(br.readLine());

        System.out.print("Enter the number of times compounded: ");     // inputs number of compounds in a year.
        compounds=Float.parseFloat(br.readLine());

        System.out.print("Enter the interest rate: ");      // inputs interest rate.
        interest=Float.parseFloat(br.readLine());

        amt=(float)principal*Math.pow(1+interest,years);

        System.out.print("\nSaving $" + principal " for " + years " years at " + roundMyDecimal.format (interest) " interest compounded " + compounds " times a year will result in $" + amount);
    }
}

Edited by Dani: Formatting fixed

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by KAUL
0

You haven't been careful with construction of output string, plus there is one use of wrong variable name. Corrected program here

import java.io.*;
import java.text.*;

public class Program08
{
    public static void main (String args[])
    throws java.io.IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        DecimalFormat roundMyDecimal=new DecimalFormat("0.00");

        float amount;
        float principal;
        float interest;
        float compounds;
        float years;

        System.out.print("Enter the principal: "); // inputs principal.
        principal=Float.parseFloat(br.readLine());

        System.out.print("Enter the number of years: "); // inputs years.
        years=Float.parseFloat(br.readLine());

        System.out.print("Enter the number of times compounded: "); // inputs number of compounds in a year.
        compounds=Float.parseFloat(br.readLine());

        System.out.print("Enter the interest rate: "); // inputs interest rate.
        interest=Float.parseFloat(br.readLine());

        /*used wrong variable name amt*/amount=principal*(float)Math.pow(1+interest,years);

        System.out.print("\nSaving $" + principal + /*missing plus*/" for " 
            + years  + /*missing plus*/" years at " + roundMyDecimal.format (interest)  + /*missing plus*/" interest compounded " 
            + compounds  + /*missing plus*/" times a year will result in $" + amount);
    }
} 

PS: When you next time provide some code please wrap it in code tags. Click on code button on menu, this will place code tags for you in editing area and then all you have do is place your code between these tags.

Edited by happygeek: fixed formatting

0

thnx u for ur help and i will do wat u told me to do next time

Edited by happygeek: fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.