Write a program that outputs the balance of a savings account at the end of every month.
First, prompt the user for the following information:
Amount of money to deposit every month: <a positive integer input>
Annual interest rate (%): <a positive integer input>
Note: You can only use getchar() to retrieve input, and putchar() or printf() to print output in this lab exercise. Using other functions, such as atoi(), will result in an automatic zero. Newline character or EOF character will serve as an indication of the end of each input. You can also assume all user inputs will be valid positive integers.
Second, compute the account balance at the end of every month for 12 months and store the data in an array. In every month, the user will deposit a sum of money into the account (given by the first input), and the bank will pay interest to the account (given by the second input).
Here is how to compute the interest: Let say the user entered a value 5 as the second input. The interest rate each month will be 0.05 / 12. Let say the user has $1000 in his/her account, the interest payment will be $1000 × 0.05 / 12 = $4.17. At the end of the month, the user will have $1004.17 in his/her account.
Third, print out the balance of the account at the end of each month, starting from the 12th month, in a tabular format, where the first and second show the month number and the balance respectively.
You should only output 2 digits after the decimal points for the account balance.
The following is a sample output of your program (bold characters are what you type):
lore 46 % make gcc lab02.c -o lab02.out lore 47 % ./lab02.out Amount of money to deposit every month: 200 Annual interest rate (%): 2 Month Balance ================== 12 2426.16 11 2222.12 10 2018.43 9 1815.07 8 1612.05 7 1409.36 6 1207.02 5 1005.01 4 803.34 3 602.00 2 401.00 1 200.33 lore 48 %
You are required to read numbers from the keyboard. To do this, use the getchar() function to read
a character from the keyboard, then convert the character to an integer value. The getchar() function
returns the ASCII code of the character pressed on the keyboard. Use the following to convert the ASCII value
to the equivalent integer value:
int n = (int)getchar() - '0';
The statement above converts the output of getchar() to an integer by shifting it's ASCII value by 48 (The ASCII code of '0' is 48, check out the ASCII table). Therefore you will get a zero for the character '0', a one for the character '1' and so forth. After converting to an integer, it is a good practice to check if the input is valid. To do this, you check the value of n to ensure it is between 0 and 9. If you are not sure how this works, feel free to approach your TA and they will be more than happy to help.
When you are satisfied that your program works correctly (or you run out of time), please do the following.
Goto the parent directory of lab02 and type the following command into the terminal:
turnin -c cs240=XXXX -p lab02 lab02
where XXXX represents your lab section number.
The turnin section is as follows:
| Section | Time | TA |
| 0201 | Thursday 15:30-17:20 | Dan Zhang |
| 0301 | Friday 09:30-11:20 | Suli Xi |
| 0401 | Friday 13:30-15:20 | Youhan Fang |
| 0501 | Thursday 09:30-11:20 | J. C. Chin |
Make sure turnin reports that your project was submitted for grading. You can check the files you have submitted by running the following command:
turnin -c cs240=XXXX -v -p lab02
| 2 points | A working Makefile is provided. |
| 4 points | Program gets user input correctly. |
| 5 points | Finding balance correctly. |
| 4 points | Printing balance correctly. |
| 5 points | Printing balance starting from 12th month. |