If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Delphi, C etc > C program help!!!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-21-04, 01:52
Jedijacob Jedijacob is offline
Registered User
 
Join Date: Sep 2004
Posts: 1
Exclamation C program help!!!

Hi, I was told to make a perfect number program in C. We began the semester less than a month ago, so we are using nothing very complicated, but I am having trouble getting it to work. It is supposed to have no input..just output and list all perfect numbers between 1 and LIMIT, which equals 10,000. Can someone show me how I could possibly do this? Your help is greatly appreciated!!!

Thanks,
Jedijacob
Reply With Quote
  #2 (permalink)  
Old 09-30-04, 15:22
Futino Futino is offline
Registered User
 
Join Date: Sep 2004
Posts: 2
There probably is a better way to do it, but the only one I can think of is the following:

int isPerfect(int n)`
{
sum=1;
for (int i=2;i<n;i++)
if (n%i==0) sum+=i;
return sum==n;
}

void main(void)
{
#define LIMIT 10000
for (int i=1;i<=LIMIT;i++)
if (isPerfect(i)) printf("%d\t",i);
}

I haven't tested it (sorry, I'm not able to test it now), and it is obviously a time consuming algorithm. I do believe it works. Hope it helps.
Reply With Quote
  #3 (permalink)  
Old 10-03-04, 06:43
kristy kristy is offline
Registered User
 
Join Date: Oct 2004
Posts: 2
Just make sure you don't use void main(), since main never should be void. main is 'special', and the same rules, regarding void, does not apply to main. On some special systems void main is allowed, but a good thumb rule is to always define a return value, even though you may not use it. Under the C99 standard, int main(void) is actually the only acceptable (together with argc, argv of course), but the following code is ok:
Code:
int main() {
  return 0;
}
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On