![]() |
| | #1 |
| Registered User Join Date: Mar 2009
Posts: 39
| can i ask what is wrong with this code? [LOOPS only] 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);
}
|
| Huskar is offline | |
| | #2 |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| LOOK at this Code: for(i = 0, j = 0; i < ( n - 1 ); i++, j++);
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #3 |
| Registered User Join Date: Mar 2009
Posts: 39
| hmm... can u suggest something please? |
| Huskar is offline | |
| | #4 |
| CSharpener Join Date: Oct 2006
Posts: 5,242
|
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #5 |
| Wheres the lesbians? Join Date: Oct 2006 Location: UK
Posts: 1,219
| 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.
__________________ Senior highbrow doctor of authority. |
| mike_g is offline | |
| | #6 |
| Registered User Join Date: Mar 2009
Posts: 39
| thanks mike... i never reviewed the syntax since it compiled properly and was ran properly... im polishing it now... thanks a lot! =) |
| Huskar is offline | |
| | #7 |
| Resu Deretsiger Join Date: Nov 2008 Location: /dev/null
Posts: 185
| 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.
__________________ Do as I say, not as I do . . . Experimentation is the essence of programming. Just remember to make a backup first. "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF Questions posted by these guidelines are more likely to be answered. Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator. |
| Nightowl is offline | |
![]() |
| Tags |
| code, loops, problems, program |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| what is wrong in this simple code | vikingcarioca | C Programming | 4 | 04-23-2009 07:10 AM |
| Error msg regarding the Switch loop, What's wrong in my code? | althagafi | C Programming | 7 | 08-06-2004 09:14 AM |
| what is wrong with this code please | korbitz | Windows Programming | 3 | 03-05-2004 10:11 AM |
| what's wrong with the following code? | catalyst | C Programming | 1 | 11-07-2003 04:30 AM |
| Interface Question | smog890 | C Programming | 11 | 06-03-2002 05:06 PM |