The program below is the solution to Liang’s Introduction to Java Programming (9th Edition) Chapter 2 Exercise 2.20.
Question: If you know the balance and the annual percentage interest rate, you can compute the interest on the next monthly payment using the following formula:
interest = balance * (annualInterestRate /1200)
Write a program that reads the balance and the annual percentage interest rate and displays the interest for the next month.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 2.20 - Financial application: calculate interest<br /> *<br /> */ <br /><br />import java.util.Scanner;<br /><br />public class Ex02_20 {<br /><br /> public static void main(String[] args) {<br /> <br /> //Display Program Information<br /> System.out.println("This Program Calculates Interest On The Next Monthly"<br /> + " Payment.n");<br /><br /> //create Scanner <br /> Scanner input = new Scanner(System.in);<br /><br /> //prompt user to enter details<br /> System.out.print("Enter balance and annual percentage interest rate "<br /> + "(e.g., 3 for 3%): ");<br /> double balance = input.nextDouble();<br /> double annualInterestRate = input.nextDouble();<br /><br /> //calculate interest using the formula interest = balance * (annualInterestRate / 1200)<br /> double interest = balance * (annualInterestRate / 1200);<br /> //format interest to five decimal places<br /> interest = (int)(interest * 100000)/100000.0;<br /><br /> //display the result<br /> System.out.println("The interest is " + interest + "n");<br /> }<br />}<br /> |
Program output |
Click here to see other solutions to Introduction to Java Programming.