CS1336
Assignment #4 – Calculations and Selection Logic
Introduction
Your second programming assignment will consist of two small C++ programs. Each program will be independent of the other program. Each one should compile correctly and produce the specified output.
Please note that each of the programs should comply with the commenting and formatting rules we discussed in class. For example, there should be a header for the whole program that gives the author’s name, class name, date, and description. End braces should be commented, and there are alignment and indenting requirements as discussed. Please ask if you have any questions.
Program #1
For a more complex calculation program, please implement Problem 17 on page 146 of our text.
You may use the pow() function in the cmath header. This function has the following prototype:
double pow (double base, double power);
The “base” is the number that is being taken to some power, and the “power” is the power to which the base is taken. The function will return a double as the result.
Here is a scan from the book to help some of you who may not have the text.

package expApp;
import java.text.*;
import java.util.Scanner;
public class expApp {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.00");
double L;
double interest;
int N;
System.out.print("Please enter your loan amount: ");
L = input.nextDouble();
System.out.print("Please enter your interest rate for the loan: ");
interest = input.nextDouble();
System.out.print("Lastly how many payments will you be making: ");
N = input.nextInt();
double Rate = ((interest / 12));
double dRate = Rate / 100;
double Payments = ((dRate * Math.pow((1+dRate), N)) / ((Math.pow((1+dRate), N)) -1)) *L;
double totalPaid = (Payments * N);
double interestPaid = totalPaid - L;
System.out.println("\nLoan Amount: $ " + df.format(L));
System.out.println("Monthly interest rate: " + Math.round(Rate) +"%");
System.out.println("Number of Payments: " + N);
System.out.println("Monthly Payments of: $ " + df.format(Payments));
System.out.println("Amount Paid Back: $ " + df.format(totalPaid));
System.out.println("Interest Paid: $ " + df.format(interestPaid));
}
}
No comments:
Post a Comment