In the first round, 2 is less than 8. Then 2 plus 5 is calculated. The variable "i" should now have the value 7. At last [res += i;] is calculated. So we calculate 0 plus 7.
Problem: At the Output I do not get the number 9 but where is the error?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testing
{
class testclass
{
static void Main()
{
int res = calc(2,8,5); // from = 2; to = 8; step = 5;
Console.WriteLine(res);
Console.ReadKey();
}
static int calc(int from, int to, int step = 1)
{
int res = 0;
for (int i = from; i < to; i += step)
res += i;
return res;
}
}
}
Output: 9