In Netbeans, create a new project (called, for example, "homework02"), for this assignment. Do not use spaces in the name. Follow the same process as you did in the first assignment; if you need a refresher, please refer back to Homework 1.

Create "main.cpp" file in your project.

We're going to need to include two header files for this assignment; one for text input/output and another for some more advanced math we'll be doing. So, put the following at the top of your file:

#include <iostream>

#include <cmath>

Write an empty main() function. Remember the int return type, and go ahead and have it return 0 at the end so you don't forget it later.

You're going to write a small program that prompts the user to enter a principal amount (the starting amount), followed by a number of years, and then compute the total balance based on interest compounded once annually for that length of time. I'll give you the interest formula. For now, we'll just use a fixed rate of 5%. (But we'll change this later.)

So, we're going to need a couple variables to store values that the user is going to type in. Inside main(), declare a variable of type double named principal, and declare another one of type int named numYears. You do not have to give them initial values.

If you aren't 100% sure how to declare the variables, please refer to the class notes or textbook.

Remember how we used cout to write text output to the screen? C++ also provides cin, which lets us read values that the user types at the keyboard. The “in” stands for “input” to the program, just like “out” stands for output.

First, we want to ask the user to enter the principal amount, but before we do the input, we should output some useful text that describes what the user should enter. So, after the variable declarations above, use cout to output a prompt, like this:

cout << "Please enter the principal: ";

Notice that I included a space after the colon, and didn't put an endl at the end of the line. This means that when you run the program and are asked to enter a number, it will go on the same line as the prompt.

Next, you will use cin to input a number into the principal variable. We use cin a lot like we use cout, but we reverse the direction of the arrows to indicate which direction the data is traveling. After the line above, write:

cin >> principal;

Notice that the arrows (i.e. >> and << ) point towards the variables and constants that they are working with!

In other words, what we are saying here is read a value from cin and store it in the variable principal. Since principal is a double, whatever the user types in will be treated as a decimal number (if possible).

Now repeat this process, prompting the user for the number of years. Use cout to print an appropriate prompt, then use cin to read a value into your numYears variable.

Now that you have your input values, declare a variable named balance of type double that will hold the final balance.

Set balance to be equal to an expression that computes the balance based on the original principal and number of years, using 5% as the interest rate compounded annually. The mathematical expression for compounded interest is CB = P * (1 + r)t, where CB is the final compound balance, P is the original principal, and t is the number of years, and r is the interest rate. Note: The interest rate should be between 0 and 1; for example, 5% is 0.05. Remember that the exponent should be computed with the pow() function.

Print the final balance using cout. Don't just print the number – print a meaningful message like "The final balance is " followed by the amount. Remember that you can chain strings and other values together by using <<multiple times, so:

cout << "The final balance is " << balance << endl;

would print the balance after that message.

Compile and run your program, and make sure it works as expected. You can test it with the following values: principal = 1000, number of years = 4. (When you enter numbers larger than 1000, do not type commas.) The final balance should be 1215.51, or close to it. (Some computers might print the value out to a different number of decimal places, but that's ok.)

Part II: Changing the interest rate

Now let’s modify the program slightly, so that it asks the user for the interest rate as well. First, declare a new variable called rate that will represent the interest rate as a double. It should be declared in the same place where you declared principal and numYears.

After the part of your program where you read the number of years from the user, add another cout prompt and a cin line to ask the user for the interest rate. We want the user to enter the number as an integer percentage (without the percent sign), not as a decimal; in other words, for 5%, you would enter "5", not "0.05" or "5%".

Now, modify the formula that computes the balance to use the new rate variable instead of the fixed rate you used in Part I. In other words, change your 1 + 0.05 (or 1.05, if you wrote it that way), to read 1 + rate / 100.

Compile and re-run the program. Try it with the same values as before: principal = 1000, number of years = 4, rate = 5. Did you get the same result? If so, try it with another set of values just to be sure: principal = 7500, number of years = 10, rate = 9. The balance should roughly equal 17755.2.

Part III: Add Simple Interest Calculation

Now you need to modify the program, adding in a new calculation for simple interest. You need to also change the output for the compound balance to include “compound.” The mathematical expression for simple interest is SB = P + P * r * t , where SB is the final simple balance, P is the original principal, and t is the number of years, and r is the interest rate.

Create "main.cpp" file in your project.

We're going to need to include two header files for this assignment; one for text input/output and another for some more advanced math we'll be doing. So, put the following at the top of your file:

#include <iostream>

#include <cmath>

Write an empty main() function. Remember the int return type, and go ahead and have it return 0 at the end so you don't forget it later.

You're going to write a small program that prompts the user to enter a principal amount (the starting amount), followed by a number of years, and then compute the total balance based on interest compounded once annually for that length of time. I'll give you the interest formula. For now, we'll just use a fixed rate of 5%. (But we'll change this later.)

So, we're going to need a couple variables to store values that the user is going to type in. Inside main(), declare a variable of type double named principal, and declare another one of type int named numYears. You do not have to give them initial values.

If you aren't 100% sure how to declare the variables, please refer to the class notes or textbook.

Remember how we used cout to write text output to the screen? C++ also provides cin, which lets us read values that the user types at the keyboard. The “in” stands for “input” to the program, just like “out” stands for output.

First, we want to ask the user to enter the principal amount, but before we do the input, we should output some useful text that describes what the user should enter. So, after the variable declarations above, use cout to output a prompt, like this:

cout << "Please enter the principal: ";

Notice that I included a space after the colon, and didn't put an endl at the end of the line. This means that when you run the program and are asked to enter a number, it will go on the same line as the prompt.

Next, you will use cin to input a number into the principal variable. We use cin a lot like we use cout, but we reverse the direction of the arrows to indicate which direction the data is traveling. After the line above, write:

cin >> principal;

Notice that the arrows (i.e. >> and << ) point towards the variables and constants that they are working with!

In other words, what we are saying here is read a value from cin and store it in the variable principal. Since principal is a double, whatever the user types in will be treated as a decimal number (if possible).

Now repeat this process, prompting the user for the number of years. Use cout to print an appropriate prompt, then use cin to read a value into your numYears variable.

Now that you have your input values, declare a variable named balance of type double that will hold the final balance.

Set balance to be equal to an expression that computes the balance based on the original principal and number of years, using 5% as the interest rate compounded annually. The mathematical expression for compounded interest is CB = P * (1 + r)t, where CB is the final compound balance, P is the original principal, and t is the number of years, and r is the interest rate. Note: The interest rate should be between 0 and 1; for example, 5% is 0.05. Remember that the exponent should be computed with the pow() function.

Print the final balance using cout. Don't just print the number – print a meaningful message like "The final balance is " followed by the amount. Remember that you can chain strings and other values together by using <<multiple times, so:

cout << "The final balance is " << balance << endl;

would print the balance after that message.

Compile and run your program, and make sure it works as expected. You can test it with the following values: principal = 1000, number of years = 4. (When you enter numbers larger than 1000, do not type commas.) The final balance should be 1215.51, or close to it. (Some computers might print the value out to a different number of decimal places, but that's ok.)

Part II: Changing the interest rate

Now let’s modify the program slightly, so that it asks the user for the interest rate as well. First, declare a new variable called rate that will represent the interest rate as a double. It should be declared in the same place where you declared principal and numYears.

After the part of your program where you read the number of years from the user, add another cout prompt and a cin line to ask the user for the interest rate. We want the user to enter the number as an integer percentage (without the percent sign), not as a decimal; in other words, for 5%, you would enter "5", not "0.05" or "5%".

Now, modify the formula that computes the balance to use the new rate variable instead of the fixed rate you used in Part I. In other words, change your 1 + 0.05 (or 1.05, if you wrote it that way), to read 1 + rate / 100.

Compile and re-run the program. Try it with the same values as before: principal = 1000, number of years = 4, rate = 5. Did you get the same result? If so, try it with another set of values just to be sure: principal = 7500, number of years = 10, rate = 9. The balance should roughly equal 17755.2.

Part III: Add Simple Interest Calculation

Now you need to modify the program, adding in a new calculation for simple interest. You need to also change the output for the compound balance to include “compound.” The mathematical expression for simple interest is SB = P + P * r * t , where SB is the final simple balance, P is the original principal, and t is the number of years, and r is the interest rate.

This is not a homework doing website. Please attempt the assignment yourself and if you are struggling ask for help.

I would normally be the first to jump at helping, but I've grown tired of all the homework questions. So I will simply post this from admin:

admin wrote: |
---|

Don't post homework questionsProgrammers are good at spotting homework questions; most of us have done them ourselves. Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions. |

Topic archived. No new replies allowed.