0

How to solve this exercise? And explanation

The program you are given takes a positive number N as input. Complete the program to calculate the sum of all numbers from 1 to N inclusive. Sample Input 4 Sample Output 10 IN C# https://www.sololearn.com/discuss/2669866/?ref=app

19th Jan 2021, 10:03 AM
Carlos
5 Answers
+ 4
Carlos This is so simple. Just start loop with index 1 and add index values. int sum = 0; for(int i = 1; i <= N; i++) { sum += i; }
19th Jan 2021, 10:15 AM
A͢J
A͢J - avatar
+ 2
i is simple a new variable/counter for the for loop. for (init; condition; increment) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int N = Convert.ToInt32(Console.ReadLine()); int sum = 0; //your code goes here for (int x = 1; x <= N; x++ ) { sum = sum + x; } Console.WriteLine(sum); } } }
24th Jul 2021, 2:04 PM
Anubis
0
thanks for the reply, just one question, its “i” and arbitrary name or when do you ise it?
21st Jan 2021, 11:15 PM
Carlos
0
Wow, how simple is that! I've been wondering too much on this...
18th Sep 2021, 7:38 PM
Adam
0
I've managed to solve it too! It was kinda rough though! using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); int sum = 0; //your code goes here for(int x = 1; x <= N; x++) { sum += x; } Console.WriteLine(sum); } } }
10th Jan 2022, 11:13 PM
Ivan Ivanov
Ivan Ivanov - avatar