C Programmers, please help with small program

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
This is my second time asking for help again and I don't really like to but sometimes I just get stuck. If you guys could give me advice that would be great. So the program criteria is:

Write a program that prints all the perfect #'s less than 10,000.

A perfect # is one where the sum of its divisors is equal to the #, ex. 6=1 +2+3

this is what I got so far. I am just confused but i think what I got so far could be right.

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

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
You need to come up with a thought process for solving such problems.

Without it; programming will never be your forte.
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
yeah.. I am an industrial engineer. Programming is not too important in this major. I've never had C programming expereince before this course.. SO kind of rough on me...
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
It is the logic of tackling a problem that you need to get a handle on; not the programming of the language at this point.
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
ah hrm, u are right eagle keeper, tackling the problem. Thats where I usually struggle, is I don't know how to approach it... hrm 6,28,496, 8128. Shouldn't 1 be one also?
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
with my code, I think i need 2 if statements right? But at least am I going in the right direction?
 

JetBlack69

Diamond Member
Sep 16, 2001
4,580
1
0
Oh, and your code is very close. You are on the right track. :)

EDIT: In fact, I copy and pasted your code, made some modifications that you are thinking of to print out the numbers in my original post. :)
 

minofifa

Senior member
May 19, 2004
485
0
0
how did you figure that out JetBlack?

I was thinking about this, and i would write two functions to call:
- one would determine the prime factors of "i"
- the other would add the prime factors determined above

Then if tthat sum equals i, print it out. Finally, i would call those two above functions in a for loop that increments "i" from 0 to 10000.

My way seem a bit inefficient but i think it would work.
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
So this is the code I have now.. I am at work and not at a compiler so I can't check what its printing....

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
}
printf(" %d", SUM);
return 0;
}
 

JetBlack69

Diamond Member
Sep 16, 2001
4,580
1
0
Originally posted by: minofifa
how did you figure that out JetBlack?

I was thinking about this, and i would write two functions to call:
- one would determine the prime factors of "i"
- the other would add the prime factors determined above

Then if tthat sum equals i, print it out. Finally, i would call those two above functions in a for loop that increments "i" from 0 to 10000.

My way seem a bit inefficient but i think it would work.

That's pretty much what Azneguy1872's code does. His "%" function determines if the numbers are factors and then it adds it to SUM.
 

JetBlack69

Diamond Member
Sep 16, 2001
4,580
1
0
Originally posted by: Aznguy1872
So this is the code I have now.. I am at work and not at a compiler so I can't check what its printing....

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
}
printf(" %d", SUM);
return 0;
}


That's good, but do you always want to increment SUM?

EDIT: Also, do you always want to print SUM?
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
oh... but doesn't incrementing that mean that if the modulus of i and j = 0, it will add that # to SUM?
 

JetBlack69

Diamond Member
Sep 16, 2001
4,580
1
0
Originally posted by: Aznguy1872
oh... but doesn't incrementing that mean that if the modulus of i and j = 0, it will add that # to SUM?

Yes, but once j == i, then you increment i and start the process over again, do you want to still add to SUM then? SUM will still contain the results from the previous iteration.
 

JetBlack69

Diamond Member
Sep 16, 2001
4,580
1
0
Also, your brackets are wrong so nothing is getting done in the second for loop, but since you are not at a compiler, it's hard to debug.
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
How do my brackets look now? I am still getting some crazy errors though.

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
}
}
printf(" %d", SUM);
return 0;
}