0

The for loop 16.3

How do I use a for loop to out put the equivalent of ((N/2)(1+N)) ?

20th Nov 2021, 11:21 AM
Samuel Whyte
5 Answers
+ 3
All you need initialize an additional variable and add it with iteration variable for each iteration means inside the loop.
20th Nov 2021, 11:30 AM
Simba
Simba - avatar
+ 1
Hi Samuel Whyte Could you elaborate on your question please. Call me dumb, but I don't understand
20th Nov 2021, 11:28 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
The for loop is good for multiple out puts I’m not sure how to use it for just the one I want. I’ve got: int N = Convert.ToInt32(Console.ReadLine()); for (int sum =1; sum < N; sum+=sum) Console.WriteLine(sum); I want sum = (N/2)(1+N)
20th Nov 2021, 11:41 AM
Samuel Whyte
+ 1
Samuel Whyte I still don't understand ((N/2)(1+N)), but I think you are trying to do this: int N, i, sum; N = Convert.ToInt32(Console.ReadLine()); sum = 0; for(i=0; i<N; i++){ sum += i; } Console.WriteLine(sum);
20th Nov 2021, 11:57 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Thank you thats solved my problem
20th Nov 2021, 2:13 PM
Samuel Whyte