C Programmers, please help with small program

Page 3 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

beggerking

Golden Member
Jan 15, 2006
1,703
0
0
you need to add a ; after sum+=j

the paranthesis is recommanded but i dnt think its required.
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
So that gets rid of that error on that line, but I think we may have an extra braces... because i get an error

test.c: In function `main':
test.c:16: error: syntax error at end of input
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
Ok i ran it, it compiles fine, but doesn't print anything so from the code below. SUM never = i..

#include <stdio.h>

int main (void) {
int i, j,SUM=0;
for (i=1;i<10000; i++) {
for (j=1;j<i; j++) {
if((i % j) == 0) {
SUM+= j;
}
if (SUM == i) {
printf("%d", SUM);
}
SUM = 0;
}
return 0;
}
}
 

beggerking

Golden Member
Jan 15, 2006
1,703
0
0
yes, add a } after sum+=j;
and delete the last }

should be like this

#include <stdio.h>

int main (void) {
int i, j,SUM=0;
for (i=1;i<10000; i++) {
for (j=1;j<i; j++) {
if((i % j) == 0) {
SUM+= j;
}
}
if (SUM == i) {
printf("%d", SUM);
}
SUM = 0;
}
return 0;
}

 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
wow, it actaully works. Thanks alot guys. Im sorry if I asked alot of questions I just want to understand how it is written and why I was getting errors. Thanks beggerking and JetBlack.