+ 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

17th Apr 2017, 4:12 PM
Matin Grimes
Matin Grimes - avatar
2 Respuestas
+ 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
18th Apr 2017, 1:35 AM
Sujith k sukumar
Sujith k sukumar - avatar
0
thanks bro, it really worked.
22nd Apr 2017, 11:51 AM
Matin Grimes
Matin Grimes - avatar