+ 1
consecutive values
how can i find sum of consecutive values in a number? for instance input: output: 10 1+2+3+4 11 5+6 22 4+5+6+7 23 11+12
2 Antworten
+ 2
Hop that the following code will help you to do this:
int inputNum = int.Parse(Console.ReadLine());
List<int> possibleValues = new List<int>();
for (int i = 1; i <= inputNum / 2; i++)
{
possibleValues.Clear();
for (int j = i; j <= (inputNum / 2)+1; j++)
{
possibleValues.Add(j);
if (possibleValues.Sum() >= inputNum)
break;
}
if (possibleValues.Sum() == inputNum)
break;
}
Console.WriteLine("Result : {0}", String.Join("+", possibleValues));
Console.ReadKey();
You need to include `System.Collections.Generic;` for List and `
using System.Linq;` for extension method Sum
You can try a working example here : https://dotnetfiddle.net/wWgYW0
0
thanks bro, it really worked.