# problem , need pointers. thanks

• 09-02-2001
mobius
problem , need pointers. thanks
Hi,
This is my first time posting, i'm having some problem with some school work. I was wondering if anyone could give me some pointers. greatly appreciated. thank you in advance.

Determine the sum of integers: read in a positive integer n, determien the sum of the first n odd integers, the sum of the first n even integers, and then print out these sums.
eg: input 5.
the sum of the integers 1,3,5,7 and 9, and determine the sum of the first five even integers, the sum of the integers 2,4,6,8 and 10.

What methods should i use to solve this problem?
• 09-02-2001
Stoned_Coder
use loops.... look at this snippet..
int number,i;
printf("enter number");
scanf("%d",&number);
for (i=1;i<(2*number);i+=2)
printf("%d\n",i);

thats odd done.... now you do even...
• 09-02-2001
mobius
Hi Stoned_coder,
Thank you for your help.
Heres what I did.
/**************************************************/
int number,i,ii;
printf("You have chosen option [1].\n");
printf("\n");
printf("Please enter a positive number: ");
scanf("%d",&number);
printf("\n");
printf("The sum of odd integers are: ");
for(i=1; i<(2*number);i+=2)
printf("%d ", i);
printf("\n");

printf("The sum of even integers are: ");
for(ii=2; ii<(2*number);ii+=2)
printf("%d ", ii);
printf("\n\n");
/**************************************************/

output:
Please enter a positive number: 5

The sum of odd integers are: 1 3 5 7 9
The sum of even integers are: 2 4 6 8

-------------

Why does it only output 4 sums for the even integer? I don't get it. thanks
• 09-02-2001
Stoned_Coder
ok firstly i was only showing you the method... you are not summing up the first five numbers but printing them.... easily fixed tho.
also on the even condition.... think about it... there are 2 options...
1) condition becomes ii<=(2*number)
2) condition becomes ii<(2*number+1)

Code:

```int number,i,ii,sum; printf("You have chosen option [1].\n"); printf("\n"); printf("Please enter a positive number: "); scanf("%d",&number); printf("\n"); printf("The odd integers are: "); sum=0; for(i=1; i<(2*number);i+=2) { sum+=i; printf("%d ", i); } printf("\n"); printf("The sum is %d\n",sum); printf("The even integers are: "); sum=0; for(ii=2; ii<(2*number+1);ii+=2) { sum+=ii; printf("%d ", ii); } printf("\nThe sum is %d\n",sum);```
• 09-03-2001
mobius
ah...thanks for the help stoned_Coder, greatly appreciated. thanks mate.