0
The for loop 16.3
How do I use a for loop to out put the equivalent of ((N/2)(1+N)) ?
5 Answers
+ 3
All you need initialize an additional variable and add it with iteration variable for each iteration means inside the loop.
+ 1
Hi Samuel Whyte
Could you elaborate on your question please.
Call me dumb, but I don't understand
+ 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)
+ 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);
+ 1
Thank you thats solved my problem