# Question in c# loop

• ### Question

• i need to write a loop acourding to the Question:

A savings account in a bank pays interest at a fixed rate on the amount in the account; that is, at the end of each year a fixed percentage of the amount is added to the amount. For example, if the account was opened with a deposit of \$100,000 at an interest rate of 10%, \$10,000 would be paid in at the end of the first year (10% of \$100,000 = \$10,000), so there would now be \$110,000 in the account. At the end of the second year \$11,000 would be paid in (10% of \$110,000 = \$11,000) so there would now be \$121,000 in the account. At the end of the third year there would be \$133,100, and so on.

Write a loop in the space below to update the amount in the account for a specified number of years, using the following variables which are provided for you:

• amount is the amount used to open the account
• rate is the interest rate as an integer (so 10 means an interest rate of 10%)
• years is the number of years that the account is kept for
```using System;
class InterestCalc
{
static void Main ()
{
while ()
{
amount += ((amount * rate) / 100) * years;

}
Console.WriteLine("The amount after {0} years at {1}% is
{2}",years,rate,amount);

}
}```

Thursday, November 23, 2017 8:41 PM

• Hello c_motti,

If you just want to use "while" statement to calculate the result of interest rate.

```static void Main()
{
Boolean flag = true;
int i = 0;
while (flag)
{
amount += amount * rate / 100;
i++;
if (i == years)
flag = false;
}

Console.WriteLine("The amount after {0} years at {1}% is {2}", years, rate, amount);
}```

Result

Sincerely,

Fei Hu

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

• Proposed as answer by Friday, November 24, 2017 6:44 AM
• Marked as answer by Friday, November 24, 2017 11:13 AM
Friday, November 24, 2017 3:15 AM
• Hello,

Your post looks like programming class assignment... We really discourage freebies

because you are suppose to learn.  While the other replies are solution... They did

Looking at your assignment description and code, we know the variables 'amount' is

accumulative, 'rate' is fixed and 'years' can vary in value.  Now, we need to decide how

the loop can be controlled,i.e. 'While()'.  Because your assignment reads as "update

the amount in the account for a specified number of years". This tell us what variable

could be used as the condition for 'While()' loop, 'years'.

Example

```while ( years > 0 )
{
amount += ((amount * rate) / 100); // * years  would be for gross calculation

years--;
}```

The above code will loop While condition of variable 'years' is greater than zero. When the condition

is not then the loop will end.  With minor changes to the assignment code, we now have a solution.

While its great that the community is here and freely helps. Sometimes the quick answer isn't helpful.

Hope this helps :)

• Proposed as answer by Friday, November 24, 2017 6:44 AM
• Marked as answer by Friday, November 24, 2017 11:12 AM
Friday, November 24, 2017 6:09 AM

### All replies

•             double amount = 100000;
int years = 10;
double rate=1.10;
for (int i = 0; i <= years; i++)
{
amount=amount*rate;
}

• Edited by Thursday, November 23, 2017 10:53 PM
Thursday, November 23, 2017 9:21 PM
• Hello c_motti,

If you just want to use "while" statement to calculate the result of interest rate.

```static void Main()
{
Boolean flag = true;
int i = 0;
while (flag)
{
amount += amount * rate / 100;
i++;
if (i == years)
flag = false;
}

Console.WriteLine("The amount after {0} years at {1}% is {2}", years, rate, amount);
}```

Result

Sincerely,

Fei Hu

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

• Proposed as answer by Friday, November 24, 2017 6:44 AM
• Marked as answer by Friday, November 24, 2017 11:13 AM
Friday, November 24, 2017 3:15 AM
• Hello,

Your post looks like programming class assignment... We really discourage freebies

because you are suppose to learn.  While the other replies are solution... They did

Looking at your assignment description and code, we know the variables 'amount' is

accumulative, 'rate' is fixed and 'years' can vary in value.  Now, we need to decide how

the loop can be controlled,i.e. 'While()'.  Because your assignment reads as "update

the amount in the account for a specified number of years". This tell us what variable

could be used as the condition for 'While()' loop, 'years'.

Example

```while ( years > 0 )
{
amount += ((amount * rate) / 100); // * years  would be for gross calculation

years--;
}```

The above code will loop While condition of variable 'years' is greater than zero. When the condition

is not then the loop will end.  With minor changes to the assignment code, we now have a solution.

While its great that the community is here and freely helps. Sometimes the quick answer isn't helpful.

Hope this helps :)

• Proposed as answer by Friday, November 24, 2017 6:44 AM
• Marked as answer by Friday, November 24, 2017 11:12 AM
Friday, November 24, 2017 6:09 AM
• thank Fei

worked for me

Friday, November 24, 2017 6:11 AM
• Hello c_motti,

If you issue has been solved, you could close the thread by marking the useful reply as answer. This can be beneficial to other community members reading this thread. Thanks a lot.

Best regards,

Fei Hu

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Friday, November 24, 2017 6:20 AM
• thanks for the detailed explanation
Friday, November 24, 2017 6:28 AM
• ```            double amount = double.Parse(Console.ReadLine());
int intYrCtr = 0;
while (years>0)
{
double dIntAmt = (amount * rate) / 100;
--years;
++intYrCtr;
Console.WriteLine("The interest amount is {3} after {0} years at {1}% of {2}", intYrCtr, rate, amount, dIntAmt);
amount += dIntAmt;
}
Console.WriteLine("The amount after {0} years at {1}% is {2}",intYrCtr,rate,amount);```

Every thing is perfect as long as you share

Friday, November 24, 2017 6:48 AM