# Thread: can i ask what is wrong with this code? [LOOPS only]

1. ## can i ask what is wrong with this code? [LOOPS only]

whats wrong with this code?
when i enter 10 as N.... it displays 81
when i enter 6 as N... its displays 25

can someone please edit or explain?

thanks!

Code:
```#include <stdio.h>

void display(int n);

int main(void)
{
int n;

clrscr();

printf("\nThis program computes and displays\n");
printf("the sum of the first N perfect squares.");
printf("\n\nEnter the value of N:\t");
scanf("%d", &n);

display(n);
getche();
return 0;

}

void display(int n)
{
int i;
int j;
int sum = 0;

for(i = 0, j = 0; i < ( n - 1 ); i++, j++);
{
printf("%d +", ( j * j ));
sum = sum + (j * j);
}

printf("\n\n%d", sum);
}```

2. LOOK at this

Code:
`for(i = 0, j = 0; i < ( n - 1 ); i++, j++);`
also note that your code makes 1 iteration less than required and starts with 0 instead of 1

3. hmm... can u suggest something please?

4. Originally Posted by Huskar
hmm... can u suggest something please?
remove ;

5. In other words: Remove the semi colon. Set your loop to start at 1 and run until it equals n.

Also, since i and j are effectivly the same you should be able to remove one of the variables.

6. thanks mike...
i never reviewed the syntax since it compiled properly and was ran properly...
im polishing it now...

thanks a lot! =)

7. One way to notice this: when you entered 10, it gave you the square of 9, or (10-1). Same for when you entered 6.

This should tip you off that something is wrong with the logic, not the syntax . . . and that the error is probably something being set to 1 when it should be 0, vice versa, or a < rather than a <=, or vice versa.