In: Computer Science
Using C programming make one for loop
Description
For this problem you will be figuring out if it is more beneficial to pay off your loans first before investing or if you should only make the minimum payments and invest the rest.
Some things to pay attention to
Interest rates given are annual interests rates but we will be assuming that interest is compounded monthly so the real rates to use will be 1/12 of what we are given
We will assume that interest is compounded on our accounts before any payments or contributions are made to them
If the user has not finished paying off all of their loans before they retire a warning message should be printed
Assumptions
Input will not always be validIf invalid input is received your program should continue to ask for more input until a valid value is entered
The exception for this is if the money set aside for investments each month is less than the minimum payment on the loan. If it is the program should terminate.
White space after desired input is allowed
You will probably want to wait until after we cover how to do this in class before handling it
Valid values for inputs
Investment money: A real number >= minimum payment
If it is not the program should terminate
Loans: A real number >= 0
Annual loan interest rate: a real number >= 0
Minimum payment: a real number >= 0
Current age: an integer >= 0
Retirement age: an integer >= current age
Annual rate of return: a real number >= 0
Example 1
Enter how much money you will be putting towards
loans/retirement each month: 500
Enter how much you owe in loans: 40000
Enter the annual interest rate of the loans: 0.03
Enter your minimum monthly loan payment: 405.32
Enter your current age: 22
Enter the age you plan to retire at: 65
Enter the annual rate of return you predict for your
investments: .05
You should only make the minimum payments on your loan and
apply
the rest towards retirement.
If you do you will have $592888.96 when you retire as opposed
to
$587281.54 if you payed off your loan before investing.
Example 2
Enter how much money you will be putting towards
loans/retirement each month: 1053
Enter how much you owe in loans: 50000
Enter the annual interest rate of the loans: 0.06
Enter your minimum monthly loan payment: 350
Enter your current age: 25
Enter the age you plan to retire at: 70
Enter the annual rate of return you predict for your
investments: 0.05
You should apply all $1053.00 towards your loan before making
any investments.
If you do you will have $1651149.44 when you retire as
opposed
to $1619732.68 if you only made minimum payments.
Example 3
Enter how much
money you will be putting towards loans/retirement each
month:
50
Enter how much you owe in loans: 1000
Enter the annual interest rate of the loans: 0.05
Enter your minimum monthly loan payment: 400
You didn't set aside enough money to pay off our loans.
Ending
program.
Example 4
Enter how much money you will be putting towards
loans/retirement each month: 500
Enter how much you owe in loans: 10000
Enter the annual interest rate of the loans: .02
Enter your minimum monthly loan payment: 100
Enter your current age: 18
Enter the age you plan to retire at: 20
Enter the annual rate of return you predict for your
investments: 0.07
Warning! After you retire you will still have $7961.19 in
loans
left.
You should only make the minimum payments on your loan and
apply
the rest towards retirement.
If you do you will have $10272.41 when you retire as opposed
to
$1835.38 if you payed off your loan before investing.
Example 5
Enter how much money you will be putting towards
loans/retirement each month: bob
Enter how much money you will be putting towards
loans/retirement each month: cat
Enter how much money you will be putting towards
loans/retirement each month: -3
Enter how much money you will be putting towards
loans/retirement each month: 250
Enter how much you owe in loans: something
Enter how much you owe in loans: 25 boys
Enter how much you owe in loans: 1000
Enter the annual interest rate of the loans: ziggy
Enter the annual interest rate of the loans: -3
Enter the annual interest rate of the loans: .1
Enter your minimum monthly loan payment: 50 50
Enter your minimum monthly loan payment: 25
Enter your current age: -5
Enter your current age: 20
Enter the age you plan to retire at: 18
Enter the age you plan to retire at: 65
Enter the annual rate of return you predict for your
investments: -8
Enter the annual rate of return you predict for your
investments: 0.04
You should apply all $250.00 towards your loan before making
any
investments.
If you do you will have $371259.10 when you retire as opposed
to
$370579.15 if you only made minimum payments.
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
//functions
void userInput();
bool is_valid_formatting(int num_args_read, int
num_args_needed);
void TotalSaving(double amountAddedMonthly, double Period, double
investmentRate, double owedAmount, double monthlyLoansRate, double
minumPayment);
void OutPut(double investmentSum, double paymentAfter);
//main program
int main ()
{
userInput();
}
void userInput()
{
//variables
double addedEachMonths, loansYouOwe,
annualLoansRate, minLoanPayment, currentAge,retireAge,
annualInvestmentsRate;
int num_args_read;
//inputs and input checks
//amount added monthly
do{
do{
printf ("Enter how much money you will be putting
towards\n");
printf("loans/retirement each month: ");
num_args_read = scanf("%lf", &addedEachMonths);
}while(!(is_valid_formatting(num_args_read,1)));
}while(addedEachMonths < 0);
//owed amount
do{
do {
printf("Enter how much you owe in loans: ");
num_args_read = scanf("%lf",&loansYouOwe);
} while
(!(is_valid_formatting(num_args_read,1)));
}while(loansYouOwe < 0);
//annual interest
do{
do {
printf("Enter the annual interest rate of the loans: ");
num_args_read = scanf("%lf",&annualLoansRate);
} while
(!(is_valid_formatting(num_args_read,1)));
}while (annualLoansRate < 0);
//minimum monthlu amount
do{
do {
printf("Enter your minimum monthly loan payment: ");
num_args_read = scanf("%lf",&minLoanPayment);
} while
(!(is_valid_formatting(num_args_read,1)));
}while(minLoanPayment < 0);
if (minLoanPayment > addedEachMonths){
printf("You didn't set
aside enough money to pay off our loans. Ending program.");
exit(0);
}
//current age
do{
do {
printf("Enter your current age: ");
num_args_read = scanf("%lf",¤tAge);
} while
(!(is_valid_formatting(num_args_read,1)));
}while(currentAge < 0 && currentAge
< 100);
//retire age
do{
do{
do {
printf("Enter the age you plan to retire at: ");
num_args_read = scanf("%lf",&retireAge);
} while ((!(is_valid_formatting(num_args_read,1)) &&
(retireAge < currentAge)));
}while(retireAge <
0);
}while(retireAge < currentAge);
//investment rate
do{
do {
printf("Enter the annual rate of return you predict for your
investments: ");
num_args_read = scanf("%lf",&annualInvestmentsRate);
} while
(!(is_valid_formatting(num_args_read,1)));
}while(annualInvestmentsRate < 0 &&
annualInvestmentsRate < 100);
//terms in month
int monthlyAgeRange = (retireAge -
currentAge)*12;
TotalSaving(addedEachMonths, monthlyAgeRange, annualInvestmentsRate/12.0, loansYouOwe, annualLoansRate/12.0, minLoanPayment);
}
//amount calcualtion
void TotalSaving(double amountAddedMonthly, double Period, double
investmentRate, double owedAmount, double monthlyLoansRate, double
minumPayment){
//variables
double totalLoans = owedAmount;
double tempLoans = owedAmount;
double totalInvestment = 0.0,minPay = 0.0;
//if the amount payed using the minimum
amount
for (int i = 0; i < Period; i++){
if (totalLoans > 0
){
totalLoans = totalLoans + (totalLoans * monthlyLoansRate);
totalLoans -= minumPayment;
totalInvestment = totalInvestment + (totalInvestment *
investmentRate);
totalInvestment = totalInvestment + (amountAddedMonthly
-minumPayment);
minPay = totalInvestment;
}
else {
totalInvestment -= totalLoans;
totalLoans = 0;
totalInvestment = totalInvestment + (totalInvestment *
investmentRate);
totalInvestment += amountAddedMonthly;
minPay = totalInvestment;
}
}
totalInvestment = 0;
//if the amount payed using whole
amount
for (int i = 0; i < Period; i++) {
if (tempLoans >
0){
tempLoans = tempLoans + (tempLoans * monthlyLoansRate);
tempLoans -= amountAddedMonthly;
}
else{
totalInvestment -= tempLoans;
tempLoans = 0;
totalInvestment = totalInvestment + (totalInvestment *
investmentRate);
totalInvestment += amountAddedMonthly;
}
}
//condition output
//still owed some money
if (totalLoans > 0){
printf("Warning! After
you retire you will still have $%.2f in loans left.\n",
totalLoans);
printf("You should apply
all $%.2f towards your loan before making any
investments.\n",amountAddedMonthly);
OutPut(minPay,
totalInvestment);
}
//if payed using minimum
amount is enough
else if (totalInvestment < minPay){
printf("You should only
make the minimum payments on your loan and apply the rest towards
retirement.\n");
printf ("If you do you
will have $%.2f when you retire as opposed to $%.2f if you payed
off your loan before investing.\n",minPay, totalInvestment);
}
//when it need to payed
with the full amount
else{
printf("You should apply
all $%.2f towards your loan before making any
investments.\n",amountAddedMonthly);
OutPut(minPay,
totalInvestment);
}
}
//general output
void OutPut(double investmentSum, double paymentAfter){
printf ("If you do you will have $%.2f when you
retire as opposed to $%.2f if you only made minimum
payments.\n",paymentAfter, investmentSum);
}
//check the user input if it's valid or not
bool is_valid_formatting(int num_args_read, int
num_args_needed)
{
char new_line ='\n';
bool is_valid = true;
if (num_args_read != num_args_needed){
is_valid =false;
}
do {
scanf("%c",
&new_line);
if
(!isspace(new_line)){
is_valid =false;
}
}while(new_line != '\n');
return is_valid;
}