Question

In: Computer Science

Using C programming make one for loop Description For this problem you will be figuring out...

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.

Solutions

Expert Solution


#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",&currentAge);
        } 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;
}


Related Solutions

Problem: Make linkedList.h and linkList.c in Programming C language Project description This project will require students...
Problem: Make linkedList.h and linkList.c in Programming C language Project description This project will require students to generate a linked list of playing card based on data read from a file and to write out the end result to a file. linkedList.h Create a header file name linkedList Include the following C header files: stdio.h stdlib.h string.h Create the following macros: TRUE 1 FACES 13 SUITS 4 Add the following function prototypes: addCard displayCards readDataFile writeDataFile Add a typedef struct...
This is Python coding question, and topic is loop Can anyone help me figuring these out?...
This is Python coding question, and topic is loop Can anyone help me figuring these out? (Please do not use build in function) Thanks. 1. Write a program that prints your name 100 times to the screen. 2. Write a program that takes a string s and an integer n as parameters and prints the string s a total of in n times(once per line) 3. Write a for loop that prints all the integers from 3141 to 5926, skipping...
Description: In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language...
Description: In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language to extract all matching patterns (substrings) from a given input DNA sequence string. The alphabet for generating DNA sequences is {A, T, G, C}. Write a regular expression that represents all DNA strings that begin with ‘A’ and end with ‘T’. Note: assume empty string is not a valid string. Design a deterministic finite automaton to recognize the regular expression. Write a program which...
Java Programming - please read the description I need this description. Inside a do/ while loop...
Java Programming - please read the description I need this description. Inside a do/ while loop (while choice !='x') , create a method call that calls a switch menu with 3 cases for variable choice and X for exit application.(this is a user input) Each case calls other methods. One of those methods asks for user input float numbers to calculate addition. And the result return to another method that displays the result. Thanks for your help! Thanks for asking....
The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
I understand how to work the problem except for figuring out the z-score. Could you please...
I understand how to work the problem except for figuring out the z-score. Could you please explain how to find the z-score for the following problem? The demand during the lead time is normally distributed with a mean of 40 and a standard deviation of 4. if the company wishes to maintain a 90 percent service level, how much safety stock should be held? Mean = 40, s= 4 x= safety stock
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert...
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert while loop to for loop example 2.) pass one dimension array(and its size) to function example
I am having difficulty figuring out the rest of this problem, primarily with regards to calculating...
I am having difficulty figuring out the rest of this problem, primarily with regards to calculating APIC and Retained earnings for Stock Dividends. Also, I need assistance with the fourth problem, how to calculate the total value of shareholders' equity. Could you please help show me how to solve? Accounting for Share Transactions The shareholders' equity section of the consolidated balance sheet of Wilson Industries appeared as follows at the beginning of the year: Shareholders' Equity Class A common stock,...
**C programming language The following description has been adopted from Deitel & Deitel. One of the...
**C programming language The following description has been adopted from Deitel & Deitel. One of the most popular games of chance is a dice game called "craps," which is played in casinos and back alleys throughout the world. The rules of the game are straightforward: A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5, and 6 spots. After the dice have come to rest, the sum of the spots on the...
Problem: Construct a C program that will make use of user-defined functions, the do/while loop, the...
Problem: Construct a C program that will make use of user-defined functions, the do/while loop, the for loop, and selection. Using a do/while loop, your program will ask/prompt the user to enter in a positive value representing the number of values they wish to have processed by the program or a value to quit/exit. If the user enters a 0 or a negative number the program should exit with a message to the user indicating they chose to exit. If...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT