Suppose a fixed amount of money is deposited at the beginning of each month into
ID: 3732617 • Letter: S
Question
Suppose a fixed amount of money is deposited at the beginning of each month into an investment paying 5% interest, compounded monthly. After each deposit is made, the new balance can be calculated using the below formula.
[new balance] = 1.005 * [previous balance one month ago] + [fixed amount]
Write a program that requires two inputs: 1) fixed amount of the monthly deposit and 2) number of months to calculate.
Use a ComboBox control to present the month options. Set the DropDownStyle to DropDownList.
The possible month values are: 60, 48, 36, 24 and 12. Use a For...Loop to add the values to the control during the form load event. Initial value: 60; terminating value: 12; Step: -12
Create a button to perform the calculation. When it is clicked, check to see if the user has entered a positive numeric value. If not, alert them to the omission and set focus to the TextBox. Also confirm they chose a duration from the ComboBox. If not, display a message and set focus to the ComboBox.
After confirming that all input was provided, use a For...Loop to calculate the monthly balance using the previously mentioned formula. The value for the first month will always be exactly equal to the monthly deposit.
Hide the form's minimize and maximize buttons.
Set the form's AcceptButton property equal to the name of the "calculate" button.
Give all controls, including the form, a meaningful name.
Monthly Investment Monthly Deposit: 1000 Duration 12 Display Monthly Balances Month 1: $1,000.00 Month 2: $2,005.00 Month 3 $3,015.03 Month 4: $4,030.10 Month 5: $5,050.25 Month 6: $6,075.50 Month 7: $7,105.88Explanation / Answer
First create a new form named monthlyInterest.
Variable name for Textbox is textBox, combo box as durationBox, button as calculateBtn and ListBox as listBox only. You can change accordingly.
C# code for the form is given below:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class MonthlyInterest : Form
{
public MonthlyInterest()
{
InitializeComponent();
}
private void MonthlyInterest_Load(object sender, EventArgs e) // form load event
{
int isSelected=0; // this variable is used to check whether value is selected from the combobox or not
durationBox.DropDownStyle = ComboBoxStyle.DropDown; // set combobox as dropdown
int max = 60;
for(int i=0;i<5;i++)
{
durationBox.Items.Add(max-(i*12));
} // adds the value of combobox dynamically at form load event
}
// this event is called when the value is selected from combo box.
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
isSelected=1; // flag is set
}
// This event is called when the button is clicked
private void calculateBtn_Click(object sender, EventArgs e)
{
double fix_amt = Convert.ToDouble(textBox.Text); // fetches the value from textbox
double duration = Convert.ToDouble(durationBox.Text); // fetches the values from combobox
double new_balance = 0;
double prev_balance = 0;
if(fix_amt<0) // checks if the amount is positive numeric value
{
MessageBox.Show("Please enter the positive amount");
textBox.Focus(); // sets the focus on textbox
}
else if(!isSelected) // checks if the duration value is selected from combo box
{
MessageBox.Show("Please select duration");
durationBox.Focus(); // sets the focus on combo box
}
else // all checks are passed
{
for(int i=1;i<=duration;i++)
{
new_balance = (1.005 * prev_balance) + fix_amt;
listBox.Items.Add("Month "+i+": $"+new_balance); // adds value to list box
prev_balance = new_balance;
}
isSelected=0; // reset the flag for next time
}
}
}
}
Now to disable minimize and maximize the button from the form and to set AcceptButton property
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.