Baiba Ozola Baiba Ozola - 1 month ago 6
C# Question

I need the program to show the result after each year

It only shows the end result.

For example - "Amount after 3. year - 1100 eur"

but I need it to show the result of year 1 and 2 as well.

int i;
Double sum;

Console.Write("Starting amount: ");
Double start = Convert.ToDouble(Console.ReadLine());

Console.Write("Duration of deposit: ");
int year = Convert.ToInt32(Console.ReadLine());

Console.Write("Annual interest rate: ");
Double c = Convert.ToDouble(Console.ReadLine());

for (i = 1; i < g; i++) ;
{
Console.Write("Amount after " + i + ". year - ");
sum = a * (1 + i * c);
Console.WriteLine(sum + " eur");
}

Answer

Remove the ; after your for loop.

// No `;` at the end of the line of the loop
for(i = 1; i < 10; i++)
{
    // Iterate until condition is met
}

You even got a warning for it: enter image description here

MSDN says about the body of the for loop's body:

The body of the loop consists of a statement, an empty statement, or a block of statements, which you create by enclosing zero or more statements in braces.

If you place a ; at the end of that for loop line you are inserting an empty statement. it is like writing:

for(i = 1; i < 10; i++) ; // The empty scope is iterated until condition is met
for(i = 1; i < 10; i++) {} // Same as above

{
    //Now after loop ends the scope is executed and actually the {} is not needed  
}