• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C Programmers, please help with small program

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.
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 then {
printf(" %d", SUM);
}
sum = 0;
}
return 0;
};
 
The purpose of that loop is to check each j value to modulus with i, but any more then that I am not sure what it idoes..
 
Originally posted by: Aznguy1872
The purpose of that loop is to check each j value to modulus with i, but any more then that I am not sure what it idoes..

according to the code, there is nothing inside the brackets, so nothing gets done in that for loop. Once the loop is done, j is the value i-1, then you are checking if (i % (i-1) ==0)
 
Originally posted by: JetBlack69
Originally posted by: Aznguy1872
The purpose of that loop is to check each j value to modulus with i, but any more then that I am not sure what it idoes..

according to the code, there is nothing inside the brackets, so nothing gets done in that for loop. Once the loop is done, j is the value i-1, then you are checking if (i % (i-1) ==0)


which means I would need an if statement in the brackets for the second for loop?
 
i would take out the sum variable and sit on ur thumb a bit. then i would fix your syntax cuz there are errors all over the damn thing......re evaulate what you want to do here.... i finished writing this prog in under 35 seconds....lets go now...
 
shell:~> gcc test.c
test.c: In function `main':
test.c:3: error: syntax error before ')' token
test.c:4: error: syntax error before ')' token
test.c: At top level:
test.c:8: error: syntax error before string constant
test.c:8: warning: conflicting types for built-in function `printf'
test.c:8: warning: data definition has no type or storage class
test.c:9: warning: data definition has no type or storage class
test.c:10: error: syntax error before '}' token
 
because I am at work Im using this ssh thing to connect to a compiler. I normally use bloodshed Dev ++, when i get home later tonight I'll be able to do more there...
 
Do you know what those mean? You have the same error in line 3 and 4, they are both for loops. You should have a ; instead of a ,
 
Im sorry if I am new at this c programming, this is my first time experience with programming and I am not good with it. But I am trying my best. I am a few chapters behind because of projects and with exams coming up dont make it much easier to get caught up.
 
#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 then) {
printf("%d", SUM);
}
sum = 0;
}
return 0;
};
 
Originally posted by: beggerking
#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 then) {
printf("%d", SUM);
}
sum = 0;
}
return 0;
};


then is not a keyword.
 
I compiled it and this is the error I get:

test.c: In function `main':
test.c:7: error: syntax error before '==' token

so in line 7.. hrm
 
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 then) {
printf("%d", SUM);
}
sum = 0;
}
return 0;
};


need to close the sum+=j

the first sum = 0 is to initialize the sum variable and to set it to 0
then the remaining sum = 0 s resets the sum after every iteration.
 
Originally posted by: Aznguy1872
how come in ur code we declaring that SUM = 0 twice?

He is declaring SUM 0 a second time for the same reason he declared it 0 the first time.

If he didn't declare SUM 0 the second time, it would contain the results of the previous iteration.
 
I still get an error with that code, I changed the sum to SUM and got rid of the 'then' and i get

test.c: In function `main':
test.c:6: error: syntax error before '==' token
 
Back
Top