+ 2

About the challenge Level Points

I passed it with this... static void Main(string[] args) { int levels = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(Points(levels)); } static int Points(int levels) { //your code goes here int total = (levels + 1) * levels / 2; return total; } i was wondering if there was a way to use for or foreach method, i had try but cant figure it our. thanks if reply.

17th Jan 2021, 9:51 AM
Charlie Chiou
Charlie Chiou - avatar
4 Answers
+ 5
In description you have Total: 1+2+3 = 6 points where 1, 2 and 3 are levels. Therfore easier way is to use a for loop: int total = 0 for (int i = levels; i > 0; i--){ total += i; } return total; A foreach loop has no sense because for it will be needed an array. In such as loop will be accessed the array elements directly.
17th Jan 2021, 10:28 AM
JaScript
JaScript - avatar
+ 2
JaScript wow thanks! I tried the same code as you! Just dont know why i cant run it well at the first time...... must missing something i guess. Happy to know having the right concept. and you are right about the foreach way, totally unnessary.
20th Jan 2021, 6:27 PM
Charlie Chiou
Charlie Chiou - avatar
+ 2
Congratulation and happy coding furthermore.
20th Jan 2021, 8:41 PM
JaScript
JaScript - avatar
+ 2
using System; using System.Collections.Generic; namespace SoloLearn { class Program { static void Main(string[] args) { int levels = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(Points(levels)); } static int Points(int levels) { //your code goes here if(levels==0){ return 0; }else{ return levels + Points(levels-1); } } } } hooooraaaaaaa!!!!!!
29th Jan 2021, 12:43 AM
Kelvyn Eleazar De Oleo Taveras
Kelvyn Eleazar De Oleo Taveras - avatar