• 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

Aznguy1872

Senior member
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
}
 
You need to come up with a thought process for solving such problems.

Without it; programming will never be your forte.
 
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...
 
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.
 
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?
 
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. 🙂
 
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.
 
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;
}
 
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.
 
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?
 
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.
 
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.
 
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;
}
 
Back
Top