fork(1) download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. int main() {
  7. double loanAmount, annualRate, monthlyRate, monthlyPayment, totalPaid, interestPaid;
  8. int numPayments;
  9.  
  10. // Ask user for input
  11. cout << "Enter the loan amount: $";
  12. cin >> loanAmount;
  13.  
  14. cout << "Enter the annual interest rate (percent, e.g., 12 for 12%): ";
  15. cin >> annualRate;
  16.  
  17. cout << "Enter the number of monthly payments: ";
  18. cin >> numPayments;
  19.  
  20. // Calculate monthly interest rate (decimal)
  21. monthlyRate = annualRate / 12.0 / 100.0;
  22.  
  23. // Calculate monthly payment using formula
  24. monthlyPayment = (loanAmount * monthlyRate) / (1 - pow(1 + monthlyRate, -numPayments));
  25.  
  26. // Calculate total amount paid and interest
  27. totalPaid = monthlyPayment * numPayments;
  28. interestPaid = totalPaid - loanAmount;
  29.  
  30. // Display report
  31. cout << fixed << setprecision(2);
  32. cout << "\n--- Loan Payment Report ---\n";
  33. cout << "Loan Amount: $" << loanAmount << endl;
  34. cout << "Monthly Interest Rate: " << monthlyRate * 100 << "%" << endl;
  35. cout << "Number of Payments: " << numPayments << endl;
  36. cout << "Monthly Payment: $" << monthlyPayment << endl;
  37. cout << "Amount Paid Back: $" << totalPaid << endl;
  38. cout << "Interest Paid: $" << interestPaid << endl;
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Enter the loan amount: $Enter the annual interest rate (percent, e.g., 12 for 12%): Enter the number of monthly payments: 
--- Loan Payment Report ---
Loan Amount: $0.00
Monthly Interest Rate: 0.00%
Number of Payments: 5253
Monthly Payment: $-nan
Amount Paid Back: $-nan
Interest Paid: $-nan