# Thread: simple summation question

1. ## simple summation question

I'm completely new to C programming, and programming in general, and I just have a quick question.

If I am trying to find the sum of the integers from n to 2*n (ex : if n = = 5, then sum = 5 + 6 + 7 + 8 + 10) how would I write that?

Thanks for the help guys!

2. for loop. start at n and add until greater than 2*n.

3. You could also take advantage of the equivalence:

sum_up_to(n) = (n * (n + 1)) / 2

4. Originally Posted by gardhr
You could also take advantage of the equivalence:

sum_up_to(n) = (n * (n + 1)) / 2
Which while nice, doesn't help him learn loops.

Quzah.

5. I have the following code and I'm not getting the result I want and I was wondering if you guys could lead me in the right direction.

Code:
```int n;
printf("Enter Non-Zero Integer\n");
scanf("%d",n);
for(n > 0; n <= n*2; n++){
printf("%d\n", n);
break;
}```

6. Code:
`scanf("%d",n);`
Turn on your compiler warnings. You need a & there.
Code:
```for(n > 0; n <= n*2, n++){
printf("%d\n", n);
break;
}```
Why use a loop if you are just going to break out of it on the first pass?
The first part of a loop is to initialize a variable if you need to, not to test if something is true.

Quzah.

7. Originally Posted by quzah
Which while nice, doesn't help him learn loops.

Quzah.
Actually, I was just pointing it out in the event that he ever need to calculate it with large numbers (in which case a loop might be prohibitively expensive).

8. First off, thanks a lot for your help I appreciate it.

I've fixed the things you said and have this as my code now :

Code:
```int n;
printf("Enter A Non-Zero Integer\n");
scanf("%d", &n);
for( ; n < (n*2) + 1 ; n++){
printf("%d\n", n);
}```
I now just get a loop of numbers being added without stopping and I manually have to stop the loop after I execute the program.

I didn't put anything in the first part of the for loop since I already initialized n. The second part of the loop I have it running while n is less than n*2 + 1 (so if my n was 5 it should stop at 10). And of course the third part is it loops and keeps adding 1 until the second condition is reached.

9. Code:
```for( ; n < (n*2) + 1 ; n++){
printf("%d\n", n);
}```
Replace n with the value n has every time through the loop:
Code:
```n = 3
for( ... ; 3 < 3*2+1; 4 )
for( ... ; 4 < 4*2+1; 5 )
for( ... ; 5 < 5*2+1; 6 )```
See the problem yet?

Quzah.

10. As quzah stated above, you need to you a different variable to increment the loop.

11. I see..

This is my new code :

Code:
```int n, sum;
printf("Enter Non-Zero Integer\n");
scanf("%d", &n);
sum = 0;
for( ; n >= 2 *n; ++n){
sum += n;
printf("%d\n", sum);
}```
So now the loop should stop when n is greater than or equal to 2*n (so using 5, when n is 10), and until that condition is met it keeps adding one. Now whenever I run the program I enter a value and just get no sum. What am I doing wrong now?

Thanks again for all the help.

12. Originally Posted by uural4792
I see..
Clearly you don't.
Originally Posted by uural4792
What am I doing wrong now?
The same thing you were doing the last time I posted. You are changing your end-loop-here value every time you change n.
Code:
```#include<stdio.h>
int main( void )
{
int n;
for( n = 0; n < n * 2; n++ )
{
printf( "for( ...; %d < %d; %d++ )\n", n, n * 2, n );
}
return 0;
}```
Run that.

Quzah.

13. Originally Posted by uural4792
I see..
So now the loop should stop when n is greater than or equal to 2*n (so using 5, when n is 10), and until that condition is met it keeps adding one. Now whenever I run the program I enter a value and just get no sum. What am I doing wrong now?

Thanks again for all the help.
Ok... so N is 5 ... the loop should stop at 10...
but the very next thing you do is ++n ...
so what is the value of N now?
Yep it's 6 and the loop would stop at 12...

Think about this logically.... you are continuously changing the exit condition of the loop... what do you think is going to happen...

Code:
```int N = 5;

if (N > 2*N)
beep();```
Translates to...
Code:
```int N = 5;

if (5 > 10)
beep();```
What are the odds of it ever beeping?

14. Originally Posted by CommonTater
What are the odds of it ever beeping?
That depends if I can say signed overflow is defined or not.

1 in INT_MAX ?

Quzah.

15. Alright guys so I went and got some help from a TA and found the mistakes I was making. I needed another variable besides sum and n to make this work. The final code I ended up with was

Code:
``` #include <stdio.h>
int main(void)
int n, sum, i;
printf("Enter a Non-Zero Integer\n");
scanf("%d", &n);
sum = 0
for(i = n ; i <= (2*n); i++){
sum += i;
printf("sum = %d"\n, sum);
}
}```
Thanks for those that helped! I really appreciate it.

Popular pages Recent additions